Changeset 188:77c5e152a4ae in livinglogic.python.orasql

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

Add readlobs parameters that can be used to return LOBs as string in
cursor fetch calls.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • NEWS.xml

    r186 r188  
    1 <section><title>Changes in 1.15 (released 02/15/2007)</title> 
     1<section><title>Changes in 1.15 (released 02/17/2007)</title> 
    22<ulist> 
    33<item>Fixed an output bug in <filename>oradiff.py</filename> when running 
    44in full output mode.</item> 
     5<item>A parameter <arg>readlobs</arg> has been added to various constructors 
     6and methods that can be used to get small (or all) <class>LOB</class>s as 
     7strings in cursor fetch calls.</item> 
    58</ulist> 
    69</section> 
  • src/ll/orasql/__init__.py

    r186 r188  
    109109        record = cls() 
    110110        for (i, field) in enumerate(row): 
     111            if isinstance(field, LOB) and (cursor.readlobs is True or (isinstance(cursor.readlobs, (int, long)) and field.size() <= cursor.readlobs)): 
     112                field = field.read() 
    111113            record[cursor.description[i][0]] = field 
    112114        return record 
     
    192194    <class>Connection</class> is a subclass of <class>cx_Oracle.Connection</class>. 
    193195    """ 
     196    def __init__(self, *args, **kwargs): 
     197        """ 
     198        Create a new connection. In addition to the parameters supported 
     199        by <function>cx_Oracle.connect</function> the keyword argument 
     200        <arg>readlobs</arg> is supported. If <arg>readlobs</arg> is <lit>False</lit> 
     201        all cursor fetch return <class>LOB</class> objects as usual. If 
     202        <arg>readlobs</arg> is an <class>int</class> (or <class>long</class>) 
     203        <class>LOB</class>s with a maximum size of <arg>readlobs</arg> 
     204        will be returned as strings. If <arg>readlobs</arg> is <lit>True</lit> 
     205        all <class>LOB</class>s will be returned as strings. 
     206        """ 
     207        if "readlobs" in kwargs: 
     208            kwargs = kwargs.copy() 
     209            self.readlobs = kwargs.pop("readlobs") 
     210        else: 
     211            self.readlobs = False 
     212        super(Connection, self).__init__(*args, **kwargs) 
     213 
    194214    def connectstring(self): 
    195215        return "%s@%s" % (self.username, self.tnsentry) 
    196216 
    197     def cursor(self): 
    198         return Cursor(self) 
     217    def cursor(self, readlobs=None): 
     218        """ 
     219        Return a new cursor for this connection. For the meaning 
     220        of <arg>readlobs</arg> see 
     221        <pyref method="__init__"><method>__init__</method></pyref>. 
     222        """ 
     223        return Cursor(self, readlobs=readlobs) 
    199224 
    200225    def __repr__(self): 
     
    470495    cursor will be returned as <pyref class="Record"><class>Record</class> objects. 
    471496    """ 
     497    def __init__(self, connection, readlobs=None): 
     498        """ 
     499        Return a new cursor for the connection <arg>connection</arg>. 
     500        For the meaning of <arg>readlobs</arg> see 
     501        <pyref class="Connection" method="__init__"><class>Connection</class>s constructor</pyref>. 
     502        """ 
     503        super(Cursor, self).__init__(connection) 
     504        if readlobs is not None: 
     505            self.readlobs = readlobs 
     506        else: 
     507            self.readlobs = connection.readlobs 
     508         
    472509    def fetchone(self, type=Record): 
    473510        row = super(Cursor, self).fetchone() 
  • test/test_orasql.py

    r187 r188  
    274274    assert result[1].p_out == 17 
    275275    assert result[1].p_inout == 40 
     276 
     277 
     278def test_fetch(): 
     279    for obj in objdict: 
     280        if isinstance(obj, orasql.Table): 
     281            # fetch only a few records 
     282            db = orasql.connect(dbname) 
     283            c = db.cursor() 
     284            c.execute("select * from %s" % obj.name) 
     285            c.readlobs = False 
     286            c.fetchone() 
     287            c.execute("select * from %s" % obj.name) 
     288            c.readlobs = True 
     289            c.fetchone() 
     290            break 
     291