]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add "impl" parameter to PickleType
authorjason3gb <jason3gb@gmail.com>
Thu, 24 Jun 2021 16:11:04 +0000 (12:11 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 24 Jun 2021 16:22:20 +0000 (12:22 -0400)
Add a impl parameter to :class:`_types.PickleType` constructor, allowing
any arbitary type to be used in place of the default implementation of
:class:`_types.LargeBinary`. Pull request courtesy jason3gb.

Fixes: #6646
Closes: #6657
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6657
Pull-request-sha: e49bcd368d1f71dba92225d8d6e3af2bbe7142f7

Change-Id: Ib79f3b0ebbc94393f673f5a5ba6558260083d0cf

doc/build/changelog/unreleased_14/6646.rst [new file with mode: 0644]
lib/sqlalchemy/sql/sqltypes.py
test/sql/test_types.py

diff --git a/doc/build/changelog/unreleased_14/6646.rst b/doc/build/changelog/unreleased_14/6646.rst
new file mode 100644 (file)
index 0000000..55e0ec8
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: usecase, sql
+    :tickets: 6646
+
+    Add a impl parameter to :class:`_types.PickleType` constructor, allowing
+    any arbitary type to be used in place of the default implementation of
+    :class:`_types.LargeBinary`. Pull request courtesy jason3gb.
\ No newline at end of file
index 4f8654afd9d26245f06e6a48d61ada40bf96adeb..4ae1211637ce793100962788f6f0853878b17c53 100644 (file)
@@ -1783,7 +1783,11 @@ class PickleType(TypeDecorator):
     cache_ok = True
 
     def __init__(
-        self, protocol=pickle.HIGHEST_PROTOCOL, pickler=None, comparator=None
+        self,
+        protocol=pickle.HIGHEST_PROTOCOL,
+        pickler=None,
+        comparator=None,
+        impl=None,
     ):
         """
         Construct a PickleType.
@@ -1798,12 +1802,22 @@ class PickleType(TypeDecorator):
           to compare values of this type.  If left as ``None``,
           the Python "equals" operator is used to compare values.
 
+        :param impl: A binary-storing :class:`_types.TypeEngine` class or
+          instance to use in place of the default :class:`_types.LargeBinary`.
+          For example the :class: `_mysql.LONGBLOB` class may be more effective
+          when using MySQL.
+
+          .. versionadded:: 1.4.20
+
         """
         self.protocol = protocol
         self.pickler = pickler or pickle
         self.comparator = comparator
         super(PickleType, self).__init__()
 
+        if impl:
+            self.impl = to_instance(impl)
+
     def __reduce__(self):
         return PickleType, (self.protocol, None, self.comparator)
 
index 9db0fee3b07718095eab1ca014d73c9edb87d0dc..3cbd2c07f948e63b80f8c1d57093113d848bd241 100644 (file)
@@ -3809,6 +3809,23 @@ class PickleTest(fixtures.TestBase):
         ):
             assert p1.compare_values(p1.copy_value(obj), obj)
 
+    @testing.combinations(
+        None, mysql.LONGBLOB, LargeBinary, mysql.LONGBLOB(), LargeBinary()
+    )
+    def test_customized_impl(self, impl):
+        """test #6646"""
+
+        if impl is None:
+            p1 = PickleType()
+            assert isinstance(p1.impl, LargeBinary)
+        else:
+            p1 = PickleType(impl=impl)
+
+            if not isinstance(impl, type):
+                impl = type(impl)
+
+            assert isinstance(p1.impl, impl)
+
 
 class CallableTest(fixtures.TestBase):
     @testing.provide_metadata