]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fixes: #10509, use oracle.INTERVAL for generic interval 10513/head
authorindivar <indimishra@gmail.com>
Fri, 20 Oct 2023 14:28:07 +0000 (19:58 +0530)
committerindivar <indimishra@gmail.com>
Fri, 20 Oct 2023 15:03:32 +0000 (20:33 +0530)
sqltypes.Interval was not using Oracle.INTERVAL to genrate DDL
query due to which precisions for DB was not being set.
added `adapt_emulated_to_native` method to oracle.INTERVAL to
adapt Interval to native INTERVAL class.

lib/sqlalchemy/dialects/oracle/types.py
lib/sqlalchemy/sql/sqltypes.py
test/dialect/oracle/test_types.py

index 62028c767386df9458285c2973b006673c86fbb0..860186195c6fd42d7a41496056162df94b9c5ec3 100644 (file)
@@ -203,6 +203,15 @@ class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval):
             second_precision=interval.second_precision,
         )
 
+    @classmethod
+    def adapt_emulated_to_native(
+        cls, interval: sqltypes.Interval, **kw  # type: ignore[override]
+    ):
+        return INTERVAL(
+            day_precision=interval.day_precision,
+            second_precision=interval.second_precision,
+        )
+
     @property
     def _type_affinity(self):
         return sqltypes.Interval
index 343575f196e763886ee11611c9daa66b42d1a2ad..8f7a818aaee1a2b6da9685f6bd3b3b7a0a4048ee 100644 (file)
@@ -2052,8 +2052,8 @@ class Interval(Emulated, _AbstractInterval, TypeDecorator[dt.timedelta]):
     """A type for ``datetime.timedelta()`` objects.
 
     The Interval type deals with ``datetime.timedelta`` objects.  In
-    PostgreSQL, the native ``INTERVAL`` type is used; for others, the
-    value is stored as a date which is relative to the "epoch"
+    PostgreSQL and Oracle, the native ``INTERVAL`` type is used; for others,
+    the value is stored as a date which is relative to the "epoch"
     (Jan. 1, 1970).
 
     Note that the ``Interval`` type does not currently provide date arithmetic
index a970adc4bacddace0f897faa74c0d2ff46965ebe..7bf05aafafde54b415b93db4a0e06ce72aebba16 100644 (file)
@@ -183,6 +183,10 @@ class DialectTypesTest(fixtures.TestBase, AssertsCompiledSQL):
             oracle.INTERVAL(day_precision=2, second_precision=5),
             "INTERVAL DAY(2) TO SECOND(5)",
         ),
+        (
+            sqltypes.Interval(day_precision=9, second_precision=3),
+            "INTERVAL DAY(9) TO SECOND(3)",
+        ),
     )
     def test_interval(self, type_, expected):
         self.assert_compile(type_, expected)