Ticket #1258: mssql-django.patch
File mssql-django.patch, 5.6 KB (added by , 19 years ago) |
---|
-
django/core/meta/__init__.py
=== django/core/meta/__init__.py ==================================================================
995 995 record_exists = True 996 996 if pk_set: 997 997 # Determine whether a record with the primary key already exists. 998 cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \ 999 (db.db.quote_name(opts.db_table), db.db.quote_name(opts.pk.column)), [pk_val]) 998 if settings.DATABASE_ENGINE == "mssql": 999 cursor.execute("SELECT TOP 1 1 FROM %s WHERE %s=%%s " % (db.db.quote_name(opts.db_table), db.db.quote_name(opts.pk.column)), [pk_val]) 1000 else: 1001 cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \ 1002 (db.db.quote_name(opts.db_table), db.db.quote_name(opts.pk.column)), [pk_val]) 1000 1003 # If it does already exist, do an UPDATE. 1001 1004 if cursor.fetchone(): 1002 1005 db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), False)) for f in non_pks] … … 1021 1024 placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \ 1022 1025 (db.db.quote_name(opts.db_table), db.db.quote_name(opts.order_with_respect_to.column))) 1023 1026 db_values.append(getattr(self, opts.order_with_respect_to.attname)) 1027 if settings.DATABASE_ENGINE=="mssql" and opts.has_auto_field and pk_set: 1028 cursor.execute("SET IDENTITY_INSERT %s ON" % \ 1029 (db.db.quote_name(opts.db_table))) 1024 1030 cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \ 1025 1031 (db.db.quote_name(opts.db_table), ','.join(field_names), 1026 1032 ','.join(placeholders)), db_values) 1033 if settings.DATABASE_ENGINE=="mssql" and opts.has_auto_field and pk_set: 1034 cursor.execute("SET IDENTITY_INSERT %s OFF" % \ 1035 (db.db.quote_name(opts.db_table))) 1027 1036 if opts.has_auto_field and not pk_set: 1028 1037 setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column)) 1029 1038 db.db.commit() … … 1640 1649 order_by = ", ".join(order_by) 1641 1650 1642 1651 # LIMIT and OFFSET clauses 1643 if kwargs.get('limit') is not None: 1644 limit_sql = " %s " % db.get_limit_offset_sql(kwargs['limit'], kwargs.get('offset')) 1652 limit_sql = "" 1653 if settings.DATABASE_ENGINE=="mssql": 1654 # TODO: this should probably be added to the backend module for better maintenance maybe like? 1655 # select, where, limit_sql = db.limit_and_offset(kwargs.get('limit'), kwargs.get('offset'), opts, tables, select, where, order_by) 1656 # maybe someone who knows the internals of django better should decide? 1657 if kwargs.get('limit') is not None: 1658 select[0] = 'TOP %s %s' % (kwargs['limit'], select[0]) 1659 if kwargs.get('offset') is not None and kwargs['offset'] != 0: 1660 #hopefully this is correct, the problems I see here are: 1661 #if there is no PK in this table or if user adds GROUP BY, HAVING etc (can he do that?), 1662 #those should be added to the subquery too and they can't be or at least I don't see how 1663 #anyway I believe Group By doesn't make sense in get_object/list but some other where it might 1664 try: 1665 pkName = opts.pk.name #TODO: is this right? 1666 except: 1667 raise NotImplementedError, "This table (%s) must have a primary key in order to do OFFSET when using mssql database provider" % opts.db_table 1668 else: 1669 where.append("[%s].%s NOT IN (SELECT TOP %s [%s].%s FROM %s%s%s)" % (opts.db_table, pkName, kwargs['offset'], opts.db_table, pkName, ",".join(tables), (where and " WHERE " + " AND ".join(where) or ""), (order_by and " ORDER BY " + order_by or ""))) 1645 1670 else: 1646 assert kwargs.get('offset') is None, "'offset' is not allowed without 'limit'" 1647 limit_sql = "" 1671 if kwargs.get('limit') is not None: 1672 limit_sql = " %s " % db.get_limit_offset_sql(kwargs['limit'], kwargs.get('offset')) 1673 else: 1674 assert kwargs.get('offset') is None, "'offset' is not allowed without 'limit'" 1675 limit_sql = "" 1648 1676 1649 1677 return select, " FROM " + ",".join(tables) + (where and " WHERE " + " AND ".join(where) or "") + (order_by and " ORDER BY " + order_by or "") + limit_sql, params 1650 1678 -
django/core/meta/fields.py
=== django/core/meta/fields.py ==================================================================
22 22 RECURSIVE_RELATIONSHIP_CONSTANT = 'self' 23 23 24 24 # prepares a value for use in a LIKE query 25 prep_for_like_query = lambda x: str(x).replace("%", "\%").replace("_", "\_") 25 if settings.DATABASE_ENGINE == "mssql": 26 prep_for_like_query = lambda x: str(x).replace("%", "[%]").replace("_", "[_]") 27 else: 28 prep_for_like_query = lambda x: str(x).replace("%", "\%").replace("_", "\_") 26 29 27 30 # returns the <ul> class for a given radio_admin value 28 31 get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '') -
tests/runtests.py
=== tests/runtests.py ==================================================================
206 206 if __name__ == "__main__": 207 207 from optparse import OptionParser 208 208 usage = "%prog [options] [model model model ...]" 209 parser = OptionParser( )209 parser = OptionParser(usage=usage) 210 210 parser.add_option('-v', help='How verbose should the output be? Choices are 0, 1 and 2, where 2 is most verbose. Default is 0.', 211 211 type='choice', choices=['0', '1', '2']) 212 212 parser.add_option('--settings',