summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2022-04-15 00:02:56 (GMT)
committerGitHub <noreply@github.com>2022-04-15 00:02:56 (GMT)
commitee475430d431814cbb6eb5e8a6c0ae51943349d4 (patch)
treee9464bb51a6304a77da9461d28564b00689edfc1 /Doc
parentc9d41bcd68dbe339396523e931904930a87819b9 (diff)
downloadcpython-ee475430d431814cbb6eb5e8a6c0ae51943349d4.zip
cpython-ee475430d431814cbb6eb5e8a6c0ae51943349d4.tar.gz
cpython-ee475430d431814cbb6eb5e8a6c0ae51943349d4.tar.bz2
gh-69093: Support basic incremental I/O to blobs in `sqlite3` (GH-30680)
Authored-by: Aviv Palivoda <palaviv@gmail.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@innova.no> Co-authored-by: palaviv <palaviv@gmail.com> Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/includes/sqlite3/blob.py12
-rw-r--r--Doc/library/sqlite3.rst66
-rw-r--r--Doc/whatsnew/3.11.rst4
3 files changed, 82 insertions, 0 deletions
diff --git a/Doc/includes/sqlite3/blob.py b/Doc/includes/sqlite3/blob.py
new file mode 100644
index 0000000..61994fb
--- /dev/null
+++ b/Doc/includes/sqlite3/blob.py
@@ -0,0 +1,12 @@
+import sqlite3
+
+con = sqlite3.connect(":memory:")
+con.execute("create table test(blob_col blob)")
+con.execute("insert into test(blob_col) values (zeroblob(10))")
+
+blob = con.blobopen("test", "blob_col", 1)
+blob.write(b"Hello")
+blob.write(b"World")
+blob.seek(0)
+print(blob.read()) # will print b"HelloWorld"
+blob.close()
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index 60dfbef..d0274fb 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -394,6 +394,20 @@ Connection Objects
supplied, this must be a callable returning an instance of :class:`Cursor`
or its subclasses.
+ .. method:: blobopen(table, column, row, /, *, readonly=False, name="main")
+
+ Open a :class:`Blob` handle to the :abbr:`BLOB (Binary Large OBject)`
+ located in row *row*, column *column*, table *table* of database *name*.
+ When *readonly* is :const:`True` the blob is opened without write
+ permissions.
+
+ .. note::
+
+ The blob size cannot be changed using the :class:`Blob` class.
+ Use the SQL function ``zeroblob`` to create a blob with a fixed size.
+
+ .. versionadded:: 3.11
+
.. method:: commit()
This method commits the current transaction. If you don't call this method,
@@ -1088,6 +1102,58 @@ Exceptions
transactions turned off. It is a subclass of :exc:`DatabaseError`.
+.. _sqlite3-blob-objects:
+
+Blob Objects
+------------
+
+.. versionadded:: 3.11
+
+.. class:: Blob
+
+ A :class:`Blob` instance is a :term:`file-like object` that can read and write
+ data in an SQLite :abbr:`BLOB (Binary Large OBject)`. Call ``len(blob)`` to
+ get the size (number of bytes) of the blob.
+
+ .. method:: close()
+
+ Close the blob.
+
+ The blob will be unusable from this point onward. An
+ :class:`~sqlite3.Error` (or subclass) exception will be raised if any
+ further operation is attempted with the blob.
+
+ .. method:: read(length=-1, /)
+
+ Read *length* bytes of data from the blob at the current offset position.
+ If the end of the blob is reached, the data up to
+ :abbr:`EOF (End of File)` will be returned. When *length* is not
+ specified, or is negative, :meth:`~Blob.read` will read until the end of
+ the blob.
+
+ .. method:: write(data, /)
+
+ Write *data* to the blob at the current offset. This function cannot
+ change the blob length. Writing beyond the end of the blob will raise
+ :exc:`ValueError`.
+
+ .. method:: tell()
+
+ Return the current access position of the blob.
+
+ .. method:: seek(offset, origin=os.SEEK_SET, /)
+
+ Set the current access position of the blob to *offset*. The *origin*
+ argument defaults to :data:`os.SEEK_SET` (absolute blob positioning).
+ Other values for *origin* are :data:`os.SEEK_CUR` (seek relative to the
+ current position) and :data:`os.SEEK_END` (seek relative to the blob’s
+ end).
+
+ :class:`Blob` example:
+
+ .. literalinclude:: ../includes/sqlite3/blob.py
+
+
.. _sqlite3-types:
SQLite and Python types
diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index f8e86f6..dba554c 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -393,6 +393,10 @@ sqlite3
:class:`sqlite3.Connection` for creating aggregate window functions.
(Contributed by Erlend E. Aasland in :issue:`34916`.)
+* Add :meth:`~sqlite3.Connection.blobopen` to :class:`sqlite3.Connection`.
+ :class:`sqlite3.Blob` allows incremental I/O operations on blobs.
+ (Contributed by Aviv Palivoda and Erlend E. Aasland in :issue:`24905`)
+
sys
---