From e5e1ea602d9393a29400098dc40d0f9de846fdd1 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 19 Aug 2005 21:51:14 +0000 Subject: [PATCH] Fixed #350 -- 'offset' DB API parameter now works in MySQL 3. Tests from [540] pass. Thanks, ronan@cremin.com git-svn-id: http://code.djangoproject.com/svn/django/trunk@541 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/db/__init__.py | 1 + django/core/db/backends/mysql.py | 6 ++++++ django/core/db/backends/postgresql.py | 6 ++++++ django/core/db/backends/sqlite3.py | 6 ++++++ django/core/meta/__init__.py | 5 ++--- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/django/core/db/__init__.py b/django/core/db/__init__.py index 43ca2b2619..d59f44bce3 100644 --- a/django/core/db/__init__.py +++ b/django/core/db/__init__.py @@ -37,6 +37,7 @@ dictfetchall = dbmod.dictfetchall get_last_insert_id = dbmod.get_last_insert_id get_date_extract_sql = dbmod.get_date_extract_sql get_date_trunc_sql = dbmod.get_date_trunc_sql +get_limit_offset_sql = dbmod.get_limit_offset_sql get_table_list = dbmod.get_table_list get_relations = dbmod.get_relations OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index 4a54a277e4..f65398b3b6 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -71,6 +71,12 @@ def get_date_trunc_sql(lookup_type, field_name): subtractions.append(" - interval (DATE_FORMAT(%s, '%%%%m')-1) month" % field_name) return "(%s - %s)" % (field_name, ''.join(subtractions)) +def get_limit_offset_sql(limit, offset=None): + sql = "LIMIT " + if offset and offset != 0: + sql += "%s," % offset + return sql + str(limit) + def get_table_list(cursor): "Returns a list of table names in the current database." cursor.execute("SHOW TABLES") diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index 94a6ed35c3..0afeec6f30 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -71,6 +71,12 @@ def get_date_trunc_sql(lookup_type, field_name): # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) +def get_limit_offset_sql(limit, offset=None): + sql = "LIMIT %s" % limit + if offset and offset != 0: + sql += " OFFSET %s" % offset + return sql + def get_table_list(cursor): "Returns a list of table names in the current database." cursor.execute(""" diff --git a/django/core/db/backends/sqlite3.py b/django/core/db/backends/sqlite3.py index 38ce767dfb..eef596eff0 100644 --- a/django/core/db/backends/sqlite3.py +++ b/django/core/db/backends/sqlite3.py @@ -97,6 +97,12 @@ def get_date_trunc_sql(lookup_type, field_name): # sqlite doesn't support DATE_TRUNC, so we fake it as above. return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name) +def get_limit_offset_sql(limit, offset=None): + sql = "LIMIT %s" % limit + if offset and offset != 0: + sql += " OFFSET %s" % offset + return sql + def _sqlite_date_trunc(lookup_type, dt): try: dt = typecasts.typecast_timestamp(dt) diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index e50870d9c7..5846fdcc5c 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -1277,10 +1277,9 @@ def function_get_sql_clause(opts, **kwargs): # LIMIT and OFFSET clauses if kwargs.get('limit') is not None: - limit_sql = " LIMIT %s " % kwargs['limit'] - if kwargs.get('offset') is not None and kwargs['offset'] != 0: - limit_sql += "OFFSET %s " % kwargs['offset'] + limit_sql = " %s " % db.get_limit_offset_sql(kwargs['limit'], kwargs.get('offset')) else: + assert kwargs.get('offset') is None, "'offset' is not allowed without 'limit'" limit_sql = "" return select, " FROM " + ",".join(tables) + (where and " WHERE " + " AND ".join(where) or "") + (order_by and " ORDER BY " + order_by or "") + limit_sql, params