Code

Ticket #225: meta.2

File meta.2, 3.4 KB (added by anonymous, 9 years ago)
Line 
1Index: meta.py
2===================================================================
3--- meta.py     (revision 321)
4+++ meta.py     (working copy)
5@@ -1268,12 +1268,22 @@
6     order_by = ", ".join(order_by)
7 
8     # LIMIT and OFFSET clauses
9-    if kwargs.get('limit') is not None:
10-        limit_sql = " LIMIT %s " % kwargs['limit']
11-        if kwargs.get('offset') is not None and kwargs['offset'] != 0:
12-            limit_sql += "OFFSET %s " % kwargs['offset']
13+    #gheorghe:
14+    from django.conf.settings import DATABASE_ENGINE
15+    limit_sql = ""
16+    if DATABASE_ENGINE=="ado_mssql":
17+        if kwargs.get('limit') is not None:
18+            select[0] = 'TOP %s %s' % (kwargs['limit'], select[0])
19+            if kwargs.get('offset') is not None and kwargs['offset'] != 0:
20+                #the problem is if PK is not ID and also if user adds GROUP BY, HAVING etc (can a user do that?),
21+                #those should be added to the subquery too and they can't be or at least I don't know how
22+                #anyhow Group By doesn't make sence in get_object/list but some otherwhere it might
23+                where.append("[%s].id NOT IN (SELECT TOP %s [%s].id FROM %s%s%s)" % (opts.db_table, kwargs['offset'], opts.db_table, opts.db_table, (where and " WHERE " + " AND ".join(where) or ""), (order_by and " ORDER BY " + order_by or "")))
24     else:
25-        limit_sql = ""
26+        if kwargs.get('limit') is not None:
27+            limit_sql = " LIMIT %s " % kwargs['limit']
28+            if kwargs.get('offset') is not None and kwargs['offset'] != 0:
29+                limit_sql += "OFFSET %s " % kwargs['offset']
30 
31     return select, " FROM " + ",".join(tables) + (where and " WHERE " + " AND ".join(where) or "") + (order_by and " ORDER BY " + order_by or "") + limit_sql, params
32 
33@@ -1302,7 +1312,15 @@
34     if field.null:
35         kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % (opts.db_table, field.name))
36     select, sql, params = function_get_sql_clause(opts, **kwargs)
37-    sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1' % (db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name)), sql)
38+   
39+    #gheorghe
40+    from django.conf.settings import DATABASE_ENGINE
41+    if DATABASE_ENGINE=="ado_mssql":
42+        datepart = db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name))
43+        sql = 'SELECT %s %s GROUP BY %s ORDER BY %s' % (datepart, sql, datepart, datepart)
44+    else:
45+        sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1' % (db.get_date_trunc_sql(kind, '%s.%s' % (opts.db_table, field.name)), sql)
46+   
47     cursor = db.db.cursor()
48     cursor.execute(sql, params)
49     # We have to manually run typecast_timestamp(str()) on the results, because
50@@ -1814,10 +1832,18 @@
51         Field.__init__(self, name, verbose_name, **kwargs)
52 
53     def get_db_prep_lookup(self, lookup_type, value):
54+        #gheorghe: looks like SQL Server can't parse microseconds so we remove them
55+        from django.conf.settings import DATABASE_ENGINE
56+        def mystr(v):
57+            r = str(v)
58+            if DATABASE_ENGINE=="ado_mssql" and "." in r:
59+                r=r[:r.index(".")]
60+            return r
61+           
62         if lookup_type == 'range':
63-            value = [str(v) for v in value]
64+            value = [mystr(v) for v in value]
65         else:
66-            value = str(value)
67+            value = mystr(value)
68         return Field.get_db_prep_lookup(self, lookup_type, value)
69 
70     def pre_save(self, obj, value, add):