Code

Ticket #9307: oracle-pickle.diff

File oracle-pickle.diff, 1.6 KB (added by mtredinnick, 6 years ago)

Pickling support for Oracle

Line 
1diff --git a/django/db/backends/oracle/query.py b/django/db/backends/oracle/query.py
2index 85e2f68..c049004 100644
3--- a/django/db/backends/oracle/query.py
4+++ b/django/db/backends/oracle/query.py
5@@ -25,6 +25,18 @@ def query_class(QueryClass, Database):
6         pass
7 
8     class OracleQuery(QueryClass):
9+        def __reduce__(self):
10+            """
11+            Enable pickling for this class (normal pickling handling doesn't
12+            work as Python can only pickle module-level classes by default).
13+            """
14+            if hasattr(QueryClass, '__getstate__'):
15+                assert hasattr(QueryClass, '__setstate__')
16+                data = self.__getstate__()
17+            else:
18+                data = self.__dict__
19+            return (unpickle_query_class, (QueryClass,), data)
20+
21         def resolve_columns(self, row, fields=()):
22             index_start = len(self.extra_select.keys())
23             values = [self.convert_values(v, None) for v in row[:index_start]]
24@@ -143,3 +155,17 @@ def query_class(QueryClass, Database):
25 
26     _classes[QueryClass] = OracleQuery
27     return OracleQuery
28+
29+def unpickle_query_class(QueryClass):
30+    """
31+    Utility function for handling unpickling of Oracle Query subclasses. This
32+    is called by Python's unpickling functions.
33+    """
34+    # FIXME: Would be nice to not have any dependency on cx_Oracle here. Since
35+    # modules can't be pickled, we need a way to know to load the right module.
36+    import cx_Oracle
37+
38+    klass = query_class(QueryClass, cx_Oracle)
39+    return klass.__new__(klass)
40+unpickle_query_class.__safe_for_unpickling__ = True
41+