Changeset 190:d0e8b53a01ba in livinglogic.python.orasql

Show
Ignore:
Timestamp:
02/23/07 17:15:49 (13 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Tags:
rel-1-17
Message:

The readlobs and unicode parameters are now honored when calling procedures
and functions via Procedure and Function objects.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • NEWS.xml

    r189 r190  
     1<section><title>Changes in 1.17 (released 02/23/2007)</title> 
     2<ulist> 
     3<item>The <lit>readlobs</lit> and <lit>unicode</lit> parameters are now honored 
     4when calling procedures and functions via <class>Procedure</class> and 
     5<class>Function</class> objects.</item> 
     6</ulist> 
     7</section> 
     8 
     9 
    110<section><title>Changes in 1.16 (released 02/21/2007)</title> 
    211<ulist> 
  • setup.py

    r189 r190  
    4747args=dict( 
    4848    name="ll-orasql", 
    49     version="1.16", 
     49    version="1.17", 
    5050    description="Utilities for working with cx_Oracle", 
    5151    long_description=DESCRIPTION, 
     
    7676    ], 
    7777    install_requires=[ 
    78         "ll-core >= 1.7", 
    79         "cx_Oracle >= 4.2", 
     78        "ll-core >= 1.7.3", 
     79        "cx_Oracle >= 4.2.1", 
    8080    ], 
    8181    namespace_packages=["ll"], 
    8282    zip_safe=False, 
    8383    dependency_links=[ 
    84         "http://starship.python.net/crew/atuining/cx_Oracle/index.html", # cx_Oracle 
     84        "http://sourceforge.net/project/showfiles.php?group_id=84168", # cx_Oracle 
    8585    ] 
    8686) 
  • src/ll/orasql/__init__.py

    r189 r190  
    3636 
    3737 
    38 import datetime 
     38import datetime, itertools 
    3939 
    4040from cx_Oracle import * 
     
    9494 
    9595 
     96def _promotevalue(value, cursor, isblob): 
     97    if isinstance(value, LOB) and (cursor.readlobs is True or (isinstance(cursor.readlobs, (int, long)) and value.size() <= cursor.readlobs)): 
     98        value = value.read() 
     99    if isinstance(value, str) and cursor.unicode and cursor.connection.encoding and not isblob: 
     100        value = value.decode(cursor.connection.encoding) 
     101    return value 
     102 
     103 
    96104class Record(dict): 
    97105    """ 
     
    101109    Field names are case insensitive. 
    102110    """ 
     111    def __init__(self, arg=None, **kwargs): 
     112        dict.__init__(self) 
     113        self.update(arg, **kwargs) 
     114 
     115    def update(self, arg=None, **kwargs): 
     116        if arg is not None: 
     117            # if arg is a mapping use iteritems 
     118            dict.update(self, ((key.lower(), value) for (key, value) in getattr(arg, "iteritems", arg))) 
     119        dict.update(self, ((key.lower(), value) for (key, value) in kwargs.iteritems())) 
     120 
    103121    @classmethod 
    104122    def fromdata(cls, cursor, row): 
     
    107125        from the database data. 
    108126        """ 
    109         record = cls() 
    110         encoding = getattr(cursor.connection, "encoding", None) 
    111         for (i, field) in enumerate(row): 
    112             if isinstance(field, LOB) and (cursor.readlobs is True or (isinstance(cursor.readlobs, (int, long)) and field.size() <= cursor.readlobs)): 
    113                 field = field.read() 
    114             if isinstance(field, str) and cursor.unicode and encoding and cursor.description[i][1] is not BLOB: 
    115                 field = field.decode(encoding) 
    116             record[cursor.description[i][0]] = field 
    117         return record 
     127        return cls((descr[0].lower(), _promotevalue(field, cursor, descr[1] is not BLOB)) for (descr, field) in itertools.izip(cursor.description, row)) 
    118128 
    119129    def __getitem__(self, name): 
     
    143153    def __xattrs__(self, mode="default"): 
    144154        # Return the attributes of this record. This is for interfacing with ipipe 
    145         return [key.lower() for key in self.iterkeys()] 
     155        return self.iterkeys() 
    146156 
    147157    def __xrepr__(self, mode): 
     
    17331743    } 
    17341744     
    1735     def __init__(self, name, owner=None, connection=None, encoding=None): 
     1745    def __init__(self, name, owner=None, connection=None): 
    17361746        Object.__init__(self, name, owner, connection) 
    1737         self.encoding = encoding 
    17381747        self._argsbypos = None 
    17391748        self._argsbyname = None 
     
    17751784                if realargs[arginfo.position] is not _default: 
    17761785                    raise TypeError("duplicate argument for %r: %s" % (self, key)) 
    1777             if isinstance(value, unicode) and self.encoding is not None: 
    1778                 value = value.encode(self.encoding) 
     1786            if isinstance(value, unicode) and cursor.connection.encoding is not None: 
     1787                value = value.encode(cursor.connection.encoding) 
    17791788            realargs[arginfo.position] = value 
    17801789 
     
    18391848            name = "%s.%s" % (self.owner, self.name) 
    18401849 
    1841         result = Record() 
    1842         for (i, value) in enumerate(cursor.callproc(name, realargs)): 
    1843             result[self._argsbypos[i].name] = value 
    1844         return result 
     1850        return Record((self._argsbypos[i].name, _promotevalue(value, cursor, self._argsbypos[i].datatype == "blob")) for (i, value) in enumerate(cursor.callproc(name, realargs))) 
    18451851 
    18461852 
     
    18581864        or <module>ll.orasql</module> cursor. This will return a tuple containing 
    18591865        the result and a <pyref class="Record"><class>Record</class></pyref> object 
    1860         containing the modified parameters  (i.e. this record will contain all in 
     1866        containing the modified parameters (i.e. this record will contain all in 
    18611867        and out parameters). 
    18621868        """ 
     
    18711877            name = "%s.%s" % (self.owner, self.name) 
    18721878 
    1873         returnvalue = cursor.callfunc(name, self._ora2cx[self._returnvalue.datatype], realargs) 
     1879        returnvalue = _promotevalue(cursor.callfunc(name, self._ora2cx[self._returnvalue.datatype], realargs), cursor, self._returnvalue.datatype == "blob") 
    18741880        result = Record() 
    18751881        for (i, value) in enumerate(realargs): 
     
    18771883            if arginfo.isout: 
    18781884                value = value.getvalue(0) 
    1879             result[arginfo.name] = value 
     1885            result[arginfo.name] = _promotevalue(value, cursor, arginfo.datatype == "blob") 
    18801886        return (returnvalue, result) 
    18811887