Ticket #1258: mssql-django.patch

File mssql-django.patch, 5.6 KB (added by Cheng <czhang.cmu+web@…>, 18 years ago)

patch for other files

  • django/core/meta/__init__.py

    === django/core/meta/__init__.py
    ==================================================================
     
    995995    record_exists = True
    996996    if pk_set:
    997997        # 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])
    10001003        # If it does already exist, do an UPDATE.
    10011004        if cursor.fetchone():
    10021005            db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), False)) for f in non_pks]
     
    10211024            placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \
    10221025                (db.db.quote_name(opts.db_table), db.db.quote_name(opts.order_with_respect_to.column)))
    10231026            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)))
    10241030        cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
    10251031            (db.db.quote_name(opts.db_table), ','.join(field_names),
    10261032            ','.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)))
    10271036        if opts.has_auto_field and not pk_set:
    10281037            setattr(self, opts.pk.attname, db.get_last_insert_id(cursor, opts.db_table, opts.pk.column))
    10291038    db.db.commit()
     
    16401649    order_by = ", ".join(order_by)
    16411650
    16421651    # 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 "")))
    16451670    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 = ""
    16481676
    16491677    return select, " FROM " + ",".join(tables) + (where and " WHERE " + " AND ".join(where) or "") + (order_by and " ORDER BY " + order_by or "") + limit_sql, params
    16501678
  • django/core/meta/fields.py

    === django/core/meta/fields.py
    ==================================================================
     
    2222RECURSIVE_RELATIONSHIP_CONSTANT = 'self'
    2323
    2424# prepares a value for use in a LIKE query
    25 prep_for_like_query = lambda x: str(x).replace("%", "\%").replace("_", "\_")
     25if settings.DATABASE_ENGINE == "mssql":
     26    prep_for_like_query = lambda x: str(x).replace("%", "[%]").replace("_", "[_]")
     27else:
     28    prep_for_like_query = lambda x: str(x).replace("%", "\%").replace("_", "\_")
    2629
    2730# returns the <ul> class for a given radio_admin value
    2831get_ul_class = lambda x: 'radiolist%s' % ((x == HORIZONTAL) and ' inline' or '')
  • tests/runtests.py

    === tests/runtests.py
    ==================================================================
     
    206206if __name__ == "__main__":
    207207    from optparse import OptionParser
    208208    usage = "%prog [options] [model model model ...]"
    209     parser = OptionParser()
     209    parser = OptionParser(usage=usage)
    210210    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.',
    211211        type='choice', choices=['0', '1', '2'])
    212212    parser.add_option('--settings',
Back to Top