]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
SYBASE: Added offset support
authorsnowman2 <alansnow21@gmail.com>
Sat, 9 May 2020 13:17:21 +0000 (09:17 -0400)
committerGord Thompson <gord@gordthompson.com>
Sun, 24 May 2020 12:15:25 +0000 (06:15 -0600)
Fixes: #5294
### Description
Fix for https://github.com/sqlalchemy/sqlalchemy/issues/5294#issue-610311455

I tested it against our database (Adaptive Server Enterprise/16.0 SP02) with `pyodbc+sybase`

### Checklist

This pull request is:

- [x] A short code fix for #5294
- [x] Added tests.

Closes: #5312
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5312
Pull-request-sha: f62c14825f2941fdd54ede17fd8d4f105a052e05

Change-Id: Ic716e17a6f654deef78781cefc3f16333cd725c3
(cherry picked from commit 360583a00abc0bf99a29a40b918bb5f741dc5198)

doc/build/changelog/unreleased_13/5294.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sybase/base.py
test/dialect/test_sybase.py

diff --git a/doc/build/changelog/unreleased_13/5294.rst b/doc/build/changelog/unreleased_13/5294.rst
new file mode 100644 (file)
index 0000000..b564dc1
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: change, sql, sybase
+    :tickets: 5294
+
+    Added ``.offset`` support to sybase dialect.
+    Pull request courtesy Alan D. Snow.
index f0a5f18e48c616220b402b51a75d675a05886c52..e3cdce8c850f2138a13b434eac521f32894cc8d9 100644 (file)
@@ -513,28 +513,18 @@ class SybaseSQLCompiler(compiler.SQLCompiler):
         {"doy": "dayofyear", "dow": "weekday", "milliseconds": "millisecond"},
     )
 
-    def get_select_precolumns(self, select, **kw):
-        s = select._distinct and "DISTINCT " or ""
-        # TODO: don't think Sybase supports
-        # bind params for FIRST / TOP
-        limit = select._limit
-        if limit:
-            # if select._limit == 1:
-            # s += "FIRST "
-            # else:
-            # s += "TOP %s " % (select._limit,)
-            s += "TOP %s " % (limit,)
-        offset = select._offset
-        if offset:
-            raise NotImplementedError("Sybase ASE does not support OFFSET")
-        return s
-
     def get_from_hint_text(self, table, text):
         return text
 
     def limit_clause(self, select, **kw):
-        # Limit in sybase is after the select keyword
-        return ""
+        text = ""
+        if select._limit_clause is not None:
+            text += " ROWS LIMIT " + self.process(select._limit_clause, **kw)
+        if select._offset_clause is not None:
+            if select._limit_clause is None:
+                text += " ROWS"
+            text += " OFFSET " + self.process(select._offset_clause, **kw)
+        return text
 
     def visit_extract(self, extract, **kw):
         field = self.extract_map.get(extract.field, extract.field)
index 38786ab4be3eb4c40f22731eed312f8858bdeba6..642cbe4f2176674a72c8891ca5b562b0ee889046 100644 (file)
@@ -2,7 +2,6 @@ from sqlalchemy import extract
 from sqlalchemy import select
 from sqlalchemy import sql
 from sqlalchemy.databases import sybase
-from sqlalchemy.testing import assert_raises_message
 from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import fixtures
 
@@ -28,15 +27,23 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                 'SELECT DATEPART("%s", t.col1) AS anon_1 FROM t' % subst,
             )
 
-    def test_offset_not_supported(self):
-        stmt = select([1]).offset(10)
-        assert_raises_message(
-            NotImplementedError,
-            "Sybase ASE does not support OFFSET",
-            stmt.compile,
-            dialect=self.__dialect__,
+    def test_limit_offset(self):
+        stmt = select([1]).limit(5).offset(6)
+        assert stmt.compile().params == {"param_1": 5, "param_2": 6}
+        self.assert_compile(
+            stmt, "SELECT 1 ROWS LIMIT :param_1 OFFSET :param_2"
         )
 
+    def test_offset(self):
+        stmt = select([1]).offset(10)
+        assert stmt.compile().params == {"param_1": 10}
+        self.assert_compile(stmt, "SELECT 1 ROWS OFFSET :param_1")
+
+    def test_limit(self):
+        stmt = select([1]).limit(5)
+        assert stmt.compile().params == {"param_1": 5}
+        self.assert_compile(stmt, "SELECT 1 ROWS LIMIT :param_1")
+
     def test_delete_extra_froms(self):
         t1 = sql.table("t1", sql.column("c1"))
         t2 = sql.table("t2", sql.column("c1"))