Ticket #10161: 10161-r9888.diff

File 10161-r9888.diff, 5.7 KB (added by Erin Kelly, 16 years ago)

Went ahead and added a NotImplementedError for '|'

  • django/db/models/sql/expressions.py

     
    7474            if sql:
    7575                expressions.append(format % sql)
    7676                expression_params.extend(params)
    77         conn = ' %s ' % node.connector
    7877
    79         return conn.join(expressions), expression_params
     78        return connection.ops.combine_expression(node.connector, expressions), expression_params
    8079
    8180    def evaluate_leaf(self, node, qn):
    8281        if not qn:
  • django/db/backends/oracle/base.py

     
    221221        second = '%s-12-31'
    222222        return [first % value, second % value]
    223223
     224    def combine_expression(self, connector, sub_expressions):
     225        "Oracle requires special cases for %% and & operators in query expressions"
     226        if connector == '%%':
     227            return 'MOD(%s)' % ','.join(sub_expressions)
     228        elif connector == '&':
     229            return 'BITAND(%s)' % ','.join(sub_expressions)
     230        elif connector == '|':
     231            raise NotImplementedError("Bit-wise or is not supported in Oracle.")
     232        return super(DatabaseOperations, self).combine_expression(connector, sub_expressions)
    224233
    225234class DatabaseWrapper(BaseDatabaseWrapper):
    226235
  • django/db/backends/__init__.py

     
    408408        """
    409409        pass
    410410
     411    def combine_expression(self, connector, sub_expressions):
     412        """Combine a list of subexpressions into a single expression, using
     413        the provided connecting operator. This is required because operators
     414        can vary between backends (e.g., Oracle with %% and &) and between
     415        subexpression types (e.g., date expressions)
     416        """
     417        conn = ' %s ' % connector
     418        return conn.join(sub_expressions)
     419
    411420class BaseDatabaseIntrospection(object):
    412421    """
    413422    This class encapsulates all backend-specific introspection utilities
  • tests/modeltests/expressions/models.py

     
    3737>>> Company(name='Test GmbH', num_employees=32, num_chairs=1,
    3838...     ceo=Employee.objects.create(firstname='Max', lastname='Mustermann')).save()
    3939
     40>>> company_query = Company.objects.values('name','num_employees','num_chairs').order_by('name','num_employees','num_chairs')
     41
    4042# We can filter for companies where the number of employees is greater than the
    4143# number of chairs.
     44>>> company_query.filter(num_employees__gt=F('num_chairs'))
     45[{'num_chairs': 5, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 1, 'name': u'Test GmbH', 'num_employees': 32}]
    4246
    43 >>> Company.objects.filter(num_employees__gt=F('num_chairs'))
    44 [<Company: Example Inc.>, <Company: Test GmbH>]
     47# We can set one field to have the value of another field
     48# Make sure we have enough chairs
     49>>> company_query.update(num_chairs=F('num_employees'))
     503
     51>>> company_query
     52[{'num_chairs': 2300, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 3, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 32, 'name': u'Test GmbH', 'num_employees': 32}]
    4553
     54# We can perform arithmetic operations in expressions
     55# Make sure we have 2 spare chairs
     56>>> company_query.update(num_chairs=F('num_employees')+2)
     573
     58>>> company_query
     59[{'num_chairs': 2302, 'name': u'Example Inc.', 'num_employees': 2300}, {'num_chairs': 5, 'name': u'Foobar Ltd.', 'num_employees': 3}, {'num_chairs': 34, 'name': u'Test GmbH', 'num_employees': 32}]
     60
    4661# The relation of a foreign key can become copied over to an other foreign key.
    47 
    4862>>> Company.objects.update(point_of_contact=F('ceo'))
    49633
    5064
  • tests/regressiontests/expressions_regress/models.py

     
    11"""
    22Spanning tests for all the operations that F() expressions can perform.
    33"""
    4 
     4from django.conf import settings
    55from django.db import models
    66
    77#
     
    8787>>> Number.objects.get(pk=n.pk) # LH Bitwise ands on integers
    8888<Number: 40, 15.500>
    8989
    90 >>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
    91 >>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') | 48)
    92 >>> Number.objects.get(pk=n.pk) # LH Bitwise or on integers
    93 <Number: 58, 15.500>
    94 
    9590# Right hand operators
    9691
    9792>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
     
    123118>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 & F('integer'))
    124119>>> Number.objects.get(pk=n.pk) # RH Bitwise ands on integers
    125120<Number: 10, 15.500>
     121"""}
    126122
     123# Oracle doesn't support the Bitwise OR operator.
     124if settings.DATABASE_ENGINE != 'oracle':
     125    __test__['API_TESTS'] += """
     126
    127127>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
     128>>> _ = Number.objects.filter(pk=n.pk).update(integer=F('integer') | 48)
     129>>> Number.objects.get(pk=n.pk) # LH Bitwise or on integers
     130<Number: 58, 15.500>
     131
     132>>> _ = Number.objects.filter(pk=n.pk).update(integer=42, float=15.5)
    128133>>> _ = Number.objects.filter(pk=n.pk).update(integer=15 | F('integer'))
    129134>>> Number.objects.get(pk=n.pk) # RH Bitwise or on integers
    130135<Number: 47, 15.500>
    131136
    132 
    133 """}
     137"""
Back to Top