bpo-24905: Support BLOB incremental I/O in sqlite module by palaviv · Pull Request #271 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/includes/sqlite3/blob.py
12 changes: 12 additions & 0 deletions Doc/includes/sqlite3/blob_with.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sqlite3

con = sqlite3.connect(":memory:")
# creating the table
con.execute("create table test(id integer primary key, blob_col blob)")
con.execute("insert into test(blob_col) values (zeroblob(10))")
# opening blob handle
with con.open_blob("test", "blob_col", 1) as blob:
blob.write(b"Hello")
blob.write(b"World")
blob.seek(0)
print(blob.read()) # will print b"HelloWorld"
75 changes: 75 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ Connection Objects
supplied, this must be a callable returning an instance of :class:`Cursor`
or its subclasses.

.. method:: open_blob(table, column, row, *, readonly=False, dbname="main")

On success a :class:`Blob` handle to the
:abbr:`BLOB (Binary Large OBject)` located in row *row*,
column *column*, table *table* in database *dbname* will be returned.
When *readonly* is :const:`True` the BLOB is opened with read
permissions. Otherwise the BLOB has read and write permissions.

.. note::

The BLOB size cannot be changed using the :class:`Blob` class. Use
``zeroblob`` to create the blob in the wanted size in advance.

.. versionadded:: 3.10

.. method:: commit()

This method commits the current transaction. If you don't call this method,
Expand Down Expand Up @@ -853,6 +868,66 @@ Exceptions
transactions turned off. It is a subclass of :exc:`DatabaseError`.


.. _sqlite3-blob-objects:

Blob Objects
------------

.. versionadded:: 3.10

.. class:: Blob

A :class:`Blob` instance can read and write the data in the
:abbr:`BLOB (Binary Large OBject)`. The :class:`Blob` object implement both
the file and sequence protocol. For example, you can read data from the
:class:`Blob` by doing ``obj.read(5)`` or by doing ``obj[:5]``.
You can call ``len(obj)`` to get size of the BLOB.

.. method:: Blob.close()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Blob.* prefix can be removed.


Close the BLOB now (rather than whenever __del__ is called).

The BLOB will be unusable from this point forward; an
:class:`~sqlite3.Error` (or subclass) exception will be
raised if any operation is attempted with the BLOB.

.. method:: Blob.__len__()
Comment thread
palaviv marked this conversation as resolved.

Return the BLOB size.

.. method:: Blob.read([size])

Read *size* bytes of data from the BLOB at the current offset position.
If the end of the BLOB is reached we will return the data up to end of
file. When *size* is not specified or negative we will read up to end
of BLOB.

.. method:: Blob.write(data)

Write *data* to the BLOB at the current offset. This function cannot
changed BLOB length. If data write will result in writing to more
then BLOB current size an error will be raised.

.. method:: Blob.tell()

Return the current offset of the BLOB.

.. method:: Blob.seek(offset, whence=os.SEEK_SET)

Set the BLOB offset. The *whence* argument is optional and defaults to
:data:`os.SEEK_SET` or 0 (absolute BLOB positioning); other values
are :data:`os.SEEK_CUR` or 1 (seek relative to the current position) and
:data:`os.SEEK_END` or 2 (seek relative to the BLOB’s end).

:class:`Blob` example:

.. literalinclude:: ../includes/sqlite3/blob.py

A :class:`Blob` can also be used with :term:`context manager`:

.. literalinclude:: ../includes/sqlite3/blob_with.py


.. _sqlite3-types:

SQLite and Python types
Expand Down
254 changes: 250 additions & 4 deletions Lib/sqlite3/test/dbapi.py
Loading