From 71ff5e8bd06f44b7f32b0167ea9258bf37746046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez=20Moreno?= Date: Tue, 16 Jun 2020 18:00:09 +0200 Subject: [PATCH] =?UTF-8?q?Paginaci=C3=B3n=20de=20access?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlquerybuilder/__init__.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/sqlquerybuilder/__init__.py b/sqlquerybuilder/__init__.py index b6f5952..d3be42c 100644 --- a/sqlquerybuilder/__init__.py +++ b/sqlquerybuilder/__init__.py @@ -105,8 +105,8 @@ class Q(QMixin): _mode = "MYSQL" lookup_types = [ - 'icontains', 'istartswith', 'iendswith', - 'contains', 'startswith', 'endswith', + 'icontains', 'istartswith', 'iendswith', + 'contains', 'startswith', 'endswith', 'year', 'month', 'day', 'week_day', 'hour', 'minute', 'second', 'isnull', 'in'] @@ -426,6 +426,31 @@ class SQLCompiler(object): self.get_group_by(), self.get_limits(), ") as tbl_paginated WHERE ", conds] + if self.sql_mode == "ACCESS" and self._limits and \ + self._limits.start is not None and self._limits.stop is not None: + conds = [] + + if self._limits.start is not None: + conds.append("row_number > %s" % self._limits.start) + + if self._limits.stop is not None: + conds.append("row_number <= %s" % self._limits.stop) + + conds = " AND ".join(conds) + count = "(select count(*) FROM {table} as t2 WHERE t2.{id} <= {table}.{id}) as row_number".format(table=table, + id=self._order_by[0]) + + return ["SELECT * FROM (", "SELECT", self.get_columns(), ",", + count, + # "FROM", + # table, + self.get_joins(), + self.get_where(), + self.get_group_by(), + " FROM ", + table, + ") WHERE ", conds] + return sql def _compile(self): @@ -436,7 +461,7 @@ class SQLCompiler(object): __str__ = __repr__ - @property + @ property def sql(self,): return self.__str__() @@ -450,6 +475,6 @@ class Queryset(SQLCompiler, SQLQuery): class SQLModel(object): - @classproperty + @ classproperty def objects(cls): return Queryset(cls.table, getattr(cls, 'sql_mode', None))