Index: meta.py =================================================================== --- meta.py (revision 321) +++ meta.py (working copy) @@ -1268,12 +1268,22 @@ order_by = ", ".join(order_by) # 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'] + #gheorghe: + from django.conf.settings import DATABASE_ENGINE + limit_sql = "" + if DATABASE_ENGINE=="ado_mssql": + if kwargs.get('limit') is not None: + select[0] = 'TOP %s %s' % (kwargs['limit'], select[0]) + if kwargs.get('offset') is not None and kwargs['offset'] != 0: + #the problem is if PK is not ID and also if user adds GROUP BY, HAVING etc (can a user do that?), + #those should be added to the subquery too and they can't be + #or at least I don't know how + where.append("id NOT IN (SELECT TOP %s id FROM %s%s)" % (kwargs['offset'], opts.db_table, (order_by and " ORDER BY " + order_by or ""))) else: - limit_sql = "" + 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'] return select, " FROM " + ",".join(tables) + (where and " WHERE " + " AND ".join(where) or "") + (order_by and " ORDER BY " + order_by or "") + limit_sql, params @@ -1302,7 +1312,15 @@ if field.null: kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % (opts.db_table, field.name)) select, sql, params = function_get_sql_clause(opts, **kwargs) - sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1' % (db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name)), sql) + + #gheorghe + from django.conf.settings import DATABASE_ENGINE + if DATABASE_ENGINE=="ado_mssql": + datepart = db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name)) + sql = 'SELECT %s %s GROUP BY %s ORDER BY %s' % (datepart, sql, datepart, datepart) + else: + sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1' % (db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name)), sql) + cursor = db.db.cursor() cursor.execute(sql, params) # We have to manually run typecast_timestamp(str()) on the results, because