Large Object (LOB) Support

Python supports using large objects (LOB) datatypes — text and image.

Constructor:

Lob(type, obj) – creates an object holding a LOB value.

It takes the following arguments:
  • type – an type of the LOB object. It can have values TEXT or IMAGE as specified below.

  • obj – a Python buffer object. It is any object which supports the Buffer Protocol. Such objects include the built-in bytearray.

Types

TEXT type – describes text columns in the database.

IMAGE type – describes image columns in the database.

LOB Object Methods

.readinto(bytearray) – must be used for a bulk-copy-out operation to get data for a LOB object that is bound to a text or image column. The method returns the number of bytes read. It returns the None object to indicate that a column value has been completely copied. The application must call this method repeatedly until None is returned. The number of bytes read in each chunk is determined by the size of bytearray. The method takes the following argument:
  • bytearray – data from the column is read and copied this array.

LOB Columns in a Bulk Copy In Operation

For a bulk copy in operation, the application must use the LOB() constructor to mark a Python object for transfer for a text or image column.

For example:
conn = sybpydb.connect(dsn="user=sa;bulklogin=true;chainxacts=off")
cur = conn.cursor()cur.execute("create table mytable (id int, t text, i image)")
cur.close()
blk = conn.blkcursor()
blk.copy("mytable", direction="in")

# Transfer text and image data using a bytearray.
arr1 = bytearray(b"hello this is some text data")
lob1 = sybpydb.Lob(sybpydb.TEXT, arr1)
arr2 = bytearray(b"hello this is some image data")
lob2 = sybpydb.Lob(sybpydb.IMAGE, arr2)
blk.rowxfer([1, lob1, lob2])
In Python a file can be opened and read in many ways. Below is an example showing the use of memory maps to transfer files in a bulk copy operation:
# Transfer data from a file using memory maps.
fh1 = open(“file1”, "rb")
mp1 = mmap.mmap(fh1.fileno(), 0 , access=mmap.ACCESS_READ)
arr1 = bytearray(mp1)
lob1 = sybpydb.Lob(sybpydb.TEXT, arr1)
fh2 = open(“file2”, "rb")
mp2 = mmap.mmap(fh2.fileno(), 0 , access=mmap.ACCESS_READ)
arr2 = bytearray(mp2)
lob2 = sybpydb.Lob(sybpydb.IMAGE, arr2)
blk.rowxfer([2, lob1, lob2])

LOB Columns in a Bulk Copy Out Operation

For a bulk copy out operation of text and image columns, the text and image columns being transferred must reside at the end of a row.

Data for the text and image columns is returned as a LOB object. The rows from the table must be transferred one by one. After each row is transferred, the data in each LOB object in the row must be retrieved. The readinto() method must be repeatedly called until it returns the None object to indicate that a complete column value has been copied.

For example:
# Method to read data from a lob object
def getlobdata(lob):
    outarr = bytearray()
    chunk = bytearray(1024)
    while True:
        len = lob.readinto(chunk);
        if (len == None):
             break
        outarr.extend(chunk[:len])
return outarr

blk.copy("mytable", direction="out")
# The rows should be transferred one by one.
row = blk.rowxfer()
print(row[0])
# Now read the lob data for the text column column
arr1 = getlobdata(row[1])
print(arr1.decode())
# Now read the lob data for the text column column
arr2 = getlobdata(row[2])
print(arr2.decode())