]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed orm not applying fetch
authorFederico Caselli <cfederico87@gmail.com>
Fri, 3 Jun 2022 10:13:10 +0000 (12:13 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 4 Jun 2022 16:05:24 +0000 (12:05 -0400)
Fixed an issue where :meth:`_sql.GenerativeSelect.fetch` would be
ignored when executing a statement using the ORM.

Fixes: #8091
Change-Id: I6790c7272a71278e90de2529c8bc8ae89e54e288
(cherry picked from commit 526e9bb6ae025d3b8032d6efc1deb1a0f4a3dae3)

doc/build/changelog/unreleased_14/8091.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sqlite/aiosqlite.py
lib/sqlalchemy/orm/context.py
test/orm/test_core_compilation.py

diff --git a/doc/build/changelog/unreleased_14/8091.rst b/doc/build/changelog/unreleased_14/8091.rst
new file mode 100644 (file)
index 0000000..014f66a
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, orm, sql
+    :tickets: 8091
+
+    Fixed an issue where :meth:`_sql.GenerativeSelect.fetch` would not
+    be applied when executing a statement using the ORM.
index 8e621c8e2e59209678bbeb9fd3afb6c9836ac64c..9fc6d355ca87abc3b9872f8bee32853c4a6a4105 100644 (file)
@@ -210,7 +210,6 @@ class AsyncAdapt_aiosqlite_connection(AdaptedConnection):
             self._handle_exception(error)
 
     def close(self):
-        # print(">close", self)
         try:
             self.await_(self._connection.close())
         except Exception as error:
index 2e3066db937f32054a1a1b12dbf8f59dd90d0590..ab1fc4045b8414651d5b1ab03dcf9d1707262cd1 100644 (file)
@@ -1244,6 +1244,8 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
         correlate_except,
         limit_clause,
         offset_clause,
+        fetch_clause,
+        fetch_clause_options,
         distinct,
         distinct_on,
         prefixes,
@@ -1276,6 +1278,8 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
 
         statement._limit_clause = limit_clause
         statement._offset_clause = offset_clause
+        statement._fetch_clause = fetch_clause
+        statement._fetch_clause_options = fetch_clause_options
 
         if prefixes:
             statement._prefixes = prefixes
@@ -2190,6 +2194,10 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
             "prefixes": self.select_statement._prefixes,
             "suffixes": self.select_statement._suffixes,
             "group_by": self.group_by or None,
+            "fetch_clause": self.select_statement._fetch_clause,
+            "fetch_clause_options": (
+                self.select_statement._fetch_clause_options
+            ),
         }
 
     @property
index 0ebc9f6504b144e680bf37207453c359f55eecae..1457f873c5f6123f528151b0cfaee2130d795812 100644 (file)
@@ -271,6 +271,36 @@ class SelectableTest(QueryTest, AssertsCompiledSQL):
         eq_(stmt.entity_description, expected_entity)
         eq_(stmt.returning_column_descriptions, expected_returning)
 
+    def test_limit_offset_select(self):
+        User = self.classes.User
+
+        stmt = select(User.id).limit(5).offset(6)
+        self.assert_compile(
+            stmt,
+            "SELECT users.id FROM users LIMIT :param_1 OFFSET :param_2",
+            checkparams={"param_1": 5, "param_2": 6},
+        )
+
+    @testing.combinations(
+        (None, "ROWS ONLY"),
+        ({"percent": True}, "PERCENT ROWS ONLY"),
+        ({"percent": True, "with_ties": True}, "PERCENT ROWS WITH TIES"),
+    )
+    def test_fetch_offset_select(self, options, fetch_clause):
+        User = self.classes.User
+
+        if options is None:
+            stmt = select(User.id).fetch(5).offset(6)
+        else:
+            stmt = select(User.id).fetch(5, **options).offset(6)
+
+        self.assert_compile(
+            stmt,
+            "SELECT users.id FROM users OFFSET :param_1 "
+            "ROWS FETCH FIRST :param_2 %s" % (fetch_clause,),
+            checkparams={"param_1": 6, "param_2": 5},
+        )
+
 
 class ColumnsClauseFromsTest(QueryTest, AssertsCompiledSQL):
     __dialect__ = "default"