Changeset 236:bc9100e6888e in livinglogic.python.orasql

Show
Ignore:
Timestamp:
03/13/08 14:40:10 (12 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add a getobject() method to Connection and Synonym. procedure/function name are casesensitive now.

Connection.getobject() returns the schema object with a specified name.

Synonym.getobject() returns the object for which the Synonym object is a synonym.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • NEWS.rst

    r234 r236  
     1Changes in 1.21 (released 03/13/2008) 
     2------------------------------------- 
     3 
     4*   :class:`Connection` has a new method :meth:`getobject`, which returns the 
     5    schema object with a specified name. 
     6 
     7*   :class:`Synonym` has a new method :meth:`getobject`, that returns the object 
     8    for which the :class:`Synonym` object is a synonym. 
     9 
     10*   The name of :class:`Procedure` and :class:`Function` object now is case 
     11    sensitive when calling the procedure or function. 
     12 
     13 
     14 
    115Changes in 1.20 (released 02/07/2008) 
    216------------------------------------- 
  • setup.py

    r234 r236  
    5656args=dict( 
    5757    name="ll-orasql", 
    58     version="1.20", 
     58    version="1.21", 
    5959    description="Utilities for working with cx_Oracle", 
    6060    long_description=descr, 
  • src/ll/orasql/__init__.py

    r234 r236  
    6969    def __str__(self): 
    7070        return "%r not found" % self.obj 
     71 
     72 
     73class SQLNoSuchObjectError(Exception): 
     74    def __init__(self, name, owner): 
     75        self.name = name 
     76        self.owner = owner 
     77 
     78    def __str__(self): 
     79        if self.owner is None: 
     80            return "no object named %r" % (self.name, ) 
     81        else: 
     82            return "no object named %r for owner %r" % (self.name, self.owner) 
    7183 
    7284 
     
    478490                yield obj 
    479491 
     492    def getobject(self, name, owner=None): 
     493        """ 
     494        Return the object named :var:`name` from the schema. If :var:`owner` is 
     495        :const:`None` the current schema is queried, else the specified one is 
     496        used. 
     497        """ 
     498        cursor = self.cursor() 
     499        for query in ( 
     500            "select object_name, decode(owner, user, null, owner) as owner, object_type from all_objects where object_name = :object_name and owner = nvl(:owner, user)", 
     501            "select object_name, decode(owner, user, null, owner) as owner, object_type from all_objects where object_name = :object_name and lower(owner) = lower(nvl(:owner, user))", 
     502            "select object_name, decode(owner, user, null, owner) as owner, object_type from all_objects where lower(object_name) = lower(:object_name) and owner = nvl(:owner, user)", 
     503            "select object_name, decode(owner, user, null, owner) as owner, object_type from all_objects where lower(object_name) = lower(:object_name) and lower(owner) = lower(nvl(:owner, user))", 
     504        ): 
     505            cursor.xexecute(query, object_name=name, owner=owner) 
     506            rec = cursor.xfetchone() 
     507            if rec is not None: 
     508                type = rec.object_type.lower() 
     509                try: 
     510                    cls = Object.name2type[type] 
     511                except KeyError: 
     512                    raise TypeError("type %s not supported" % type) 
     513                else: 
     514                    return cls(rec.object_name, rec.owner, self) 
     515        raise SQLNoSuchObjectError(name, owner) 
     516 
     517 
    480518    def iterprivileges(self, schema="user"): 
    481519        """ 
     
    16081646            yield None 
    16091647 
     1648    def getobject(self, connection=None): 
     1649        """ 
     1650        Get the object for which :var:`self` is a synonym. 
     1651        """ 
     1652        (connection, cursor) = self.getcursor(connection) 
     1653        cursor.xexecute("select table_owner, table_name, db_link from all_synonyms where owner=nvl(:owner, user) and synonym_name=:name", owner=self.owner, name=self.name) 
     1654        rec = cursor.xfetchone() 
     1655        if rec is None: 
     1656            raise SQLObjectNotFoundError(self) 
     1657        return connection.getobject(rec.table_name, rec.table_owner) 
     1658 
    16101659 
    16111660class View(MixinNormalDates, Object): 
     
    17511800    def _calcargs(self, cursor): 
    17521801        if self._argsbypos is None: 
    1753             cursor.xexecute("select object_id from all_objects where owner=nvl(:owner, user) and lower(object_name)=lower(:name) and object_type=:type", owner=self.owner, name=self.name, type=self.type.upper()) 
     1802            cursor.xexecute("select object_id from all_objects where owner=nvl(:owner, user) and object_name=:name and object_type=:type", owner=self.owner, name=self.name, type=self.type.upper()) 
    17541803            if cursor.fetchone() is None: 
    17551804                raise SQLObjectNotFoundError(self) 
    17561805            self._argsbypos = [] 
    17571806            self._argsbyname = {} 
    1758             cursor.xexecute("select lower(argument_name) as name, lower(in_out) as in_out, lower(data_type) as datatype from all_arguments where owner=nvl(:owner, user) and lower(object_name)=lower(:name) and data_level=0 order by sequence", owner=self.owner, name=self.name) 
     1807            cursor.xexecute("select lower(argument_name) as name, lower(in_out) as in_out, lower(data_type) as datatype from all_arguments where owner=nvl(:owner, user) and object_name=:name and data_level=0 order by sequence", owner=self.owner, name=self.name) 
    17591808            i = 0 # argument position (skip return value) 
    17601809            for record in cursor.xfetch():