Ticket #1261: django_firebird_rev3077.patch

File django_firebird_rev3077.patch, 10.9 KB (added by david@…, 9 years ago)
  • db/models/base.py

     
    157157        record_exists = True
    158158        if pk_set:
    159159            # Determine whether a record with the primary key already exists.
    160             cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
     160            check_sql = "SELECT 1 FROM %s WHERE %s=%%s"
     161            if settings.DATABASE_ENGINE == "firebird":
     162                check_sql = backend.add_limit_offset_sql(check_sql, 1)
     163            else:
     164                check_sql += backend.get_limit_offset_sql(1)
     165            cursor.execute(check_sql % \
    161166                (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
    162167            # If it does already exist, do an UPDATE.
    163168            if cursor.fetchone():
     
    174179            db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
    175180            # If the PK has been manually set, respect that.
    176181            if pk_set:
    177                 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
     182                field_names += [backend.quote_name(f.column) for f in self._meta.fields if isinstance(f, AutoField)]
    178183                db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
    179184            placeholders = ['%s'] * len(field_names)
    180185            if self._meta.order_with_respect_to:
  • db/models/fields/__init__.py

     
    475475        if value is not None:
    476476            # MySQL will throw a warning if microseconds are given, because it
    477477            # doesn't support microseconds.
    478             if settings.DATABASE_ENGINE == 'mysql':
     478            if settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE == 'firebird':
    479479                value = value.replace(microsecond=0)
    480480            value = str(value)
    481481        return Field.get_db_prep_save(self, value)
     
    484484        if lookup_type == 'range':
    485485            value = [str(v) for v in value]
    486486        else:
    487             value = str(value)
     487            # kinterbasdb accepts datetime objects
     488            # but not datetime string with microseconds
     489            if settings.DATABASE_ENGINE != 'firebird':
     490                value = str(value)
    488491        return Field.get_db_prep_lookup(self, lookup_type, value)
    489492
    490493    def get_manipulator_field_objs(self):
     
    739742        if value is not None:
    740743            # MySQL will throw a warning if microseconds are given, because it
    741744            # doesn't support microseconds.
    742             if settings.DATABASE_ENGINE == 'mysql':
     745            if settings.DATABASE_ENGINE == 'mysql' or settings.DATABASE_ENGINE == 'firebird':
    743746                value = value.replace(microsecond=0)
    744747            value = str(value)
    745748        return Field.get_db_prep_save(self, value)
  • db/models/query.py

     
    22from django.db.models.fields import DateField, FieldDoesNotExist
    33from django.db.models import signals
    44from django.dispatch import dispatcher
     5from django.conf import settings
    56from django.utils.datastructures import SortedDict
    67import operator
    78import re
     
    159160        extra_select = self._select.items()
    160161
    161162        cursor = connection.cursor()
    162         select, sql, params = self._get_sql_clause()
    163         cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
     163        select, criteria, params = self._get_sql_clause()
     164        sql = "SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + criteria
     165        if settings.DATABASE_ENGINE == "firebird" and self._limit is not None:
     166            sql = backend.add_limit_offset_sql(sql, self._limit, self._offset)
     167        cursor.execute(sql, params)
    164168        fill_cache = self._select_related
    165169        index_end = len(self.model._meta.fields)
    166170        while 1:
     
    418422
    419423        # Add any additional SELECTs.
    420424        if self._select:
    421             select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()])
     425            select.extend(['(%s) %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()])
    422426
    423427        # Start composing the body of the SQL statement.
    424428        sql = [" FROM", backend.quote_name(opts.db_table)]
    425429
    426430        # Compose the join dictionary into SQL describing the joins.
    427431        if joins:
    428             sql.append(" ".join(["%s %s AS %s ON %s" % (join_type, table, alias, condition)
     432            sql.append(" ".join(["%s %s %s ON %s" % (join_type, table, alias, condition)
    429433                            for (alias, (table, join_type, condition)) in joins.items()]))
    430434
    431435        # Compose the tables clause into SQL.
     
    468472
    469473        # LIMIT and OFFSET clauses
    470474        if self._limit is not None:
    471             sql.append("%s " % backend.get_limit_offset_sql(self._limit, self._offset))
     475            if settings.DATABASE_ENGINE != "firebird":
     476                sql.append("%s " % backend.get_limit_offset_sql(self._limit, self._offset))
    472477        else:
    473478            assert self._offset is None, "'offset' is not allowed without 'limit'"
    474479
  • core/management.py

     
    66import os, re, shutil, sys, textwrap
    77from optparse import OptionParser
    88from django.utils import termcolors
     9from django.conf import settings
    910
    1011# For Python 2.3
    1112if not hasattr(__builtins__, 'set'):
     
    110111        final_output.extend(_get_sql_for_pending_references(klass, pending_references))
    111112        # Keep track of the fact that we've created the table for this model.
    112113        models_output.add(klass)
     114       
     115        if (settings.DATABASE_ENGINE == 'firebird') & (klass._meta.has_auto_field):
     116            final_output.extend(get_creation_module().create_sequence_sql(klass._meta.db_table, klass._meta.pk.column))
    113117
    114118    # Create the many-to-many join tables.
    115119    for klass in app_models:
     
    153157            # Make the definition (e.g. 'foo VARCHAR(30)') for this field.
    154158            field_output = [style.SQL_FIELD(backend.quote_name(f.column)),
    155159                style.SQL_COLTYPE(col_type % rel_field.__dict__)]
    156             field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
     160            field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or 'DEFAULT ')))
    157161            if f.unique:
    158162                field_output.append(style.SQL_KEYWORD('UNIQUE'))
    159163            if f.primary_key:
     
    204208                col = opts.get_field(f.rel.field_name).column
    205209                final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \
    206210                    (backend.quote_name(r_table),
    207                     backend.quote_name('%s_referencing_%s_%s' % (r_col, table, col)),
     211                    backend.quote_name('%s__%s' % (r_col, col)),
    208212                    backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)))
    209213            del pending_references[klass]
    210214    return final_output
     
    240244            style.SQL_FIELD(backend.quote_name(f.m2m_reverse_name()))))
    241245        table_output.append(');')
    242246        final_output.append('\n'.join(table_output))
     247        if (settings.DATABASE_ENGINE == 'firebird'):
     248            final_output.extend(get_creation_module().create_sequence_sql(f.m2m_db_table(), "id"))
    243249    return final_output
    244250
    245251def get_sql_delete(app):
     
    291297                        (style.SQL_KEYWORD('ALTER TABLE'),
    292298                        style.SQL_TABLE(backend.quote_name(table)),
    293299                        style.SQL_KEYWORD(backend.get_drop_foreignkey_sql()),
    294                         style.SQL_FIELD(backend.quote_name("%s_referencing_%s_%s" % (col, r_table, r_col)))))
     300                        style.SQL_FIELD(backend.quote_name("%s__%s" % (col, r_col)))))
    295301                del references_to_delete[klass]
    296302
    297303    # Output DROP TABLE statements for many-to-many tables.
     
    10261032    index_output = []
    10271033    for f in fields:
    10281034        field_output = [backend.quote_name(f.name), data_types[f.get_internal_type()] % f.__dict__]
    1029         field_output.append("%sNULL" % (not f.null and "NOT " or ""))
     1035        field_output.append("%sNULL" % (not f.null and "NOT " or "DEFAULT "))
    10301036        if f.unique:
    10311037            field_output.append("UNIQUE")
    10321038        if f.primary_key:
  • contrib/auth/models.py

     
    6464    date_joined = models.DateTimeField(_('date joined'), default=models.LazyDate())
    6565    groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True,
    6666        help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in."))
    67     user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True, filter_interface=models.HORIZONTAL)
     67    permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True, filter_interface=models.HORIZONTAL)
    6868    objects = UserManager()
    6969    class Meta:
    7070        verbose_name = _('user')
     
    7474        fields = (
    7575            (None, {'fields': ('username', 'password')}),
    7676            (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
    77             (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
     77            (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'permissions')}),
    7878            (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    7979            (_('Groups'), {'fields': ('groups',)}),
    8080        )
     
    160160    def get_all_permissions(self):
    161161        if not hasattr(self, '_perm_cache'):
    162162            import sets
    163             self._perm_cache = sets.Set(["%s.%s" % (p.content_type.app_label, p.codename) for p in self.user_permissions.all()])
     163            self._perm_cache = sets.Set(["%s.%s" % (p.content_type.app_label, p.codename) for p in self.permissions.all()])
    164164            self._perm_cache.update(self.get_group_permissions())
    165165        return self._perm_cache
    166166
     
    249249
    250250    def _get_user_permissions(self):
    251251        raise NotImplementedError
    252     user_permissions = property(_get_user_permissions)
     252    permissions = property(_get_user_permissions)
    253253
    254254    def has_perm(self, perm):
    255255        return False
Back to Top