Ticket #5087: oracle_textfield_lookup.2.diff

File oracle_textfield_lookup.2.diff, 5.4 KB (added by ian.g.kelly@…, 17 years ago)

New version that doesn't break everything -- I should know better than to submit patches when I don't have a test environment available

  • django/db/models/query.py

     
    777777            return SortedDict(), [], []
    778778        return joins, where2, params
    779779
    780 def get_where_clause(lookup_type, table_prefix, field_name, value):
     780def get_where_clause(lookup_type, table_prefix, field_name, value, db_type):
    781781    if table_prefix.endswith('.'):
    782782        table_prefix = backend.quote_name(table_prefix[:-1])+'.'
    783783    field_name = backend.quote_name(field_name)
     
    785785        cast_sql = backend.get_datetime_cast_sql()
    786786    else:
    787787        cast_sql = '%s'
     788    if db_type and hasattr(backend, 'get_field_cast_sql'):
     789        field_cast_sql = backend.get_field_cast_sql(db_type)
     790    else:
     791        field_cast_sql = '%s%s'
     792    field_sql = field_cast_sql % (table_prefix, field_name)
    788793    if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith') and backend.needs_upper_for_iops:
    789         format = 'UPPER(%s%s) %s'
     794        format = 'UPPER(%s) %s'
    790795    else:
    791         format = '%s%s %s'
     796        format = '%s %s'
    792797    try:
    793         return format % (table_prefix, field_name,
     798        return format % (field_sql,
    794799                         backend.OPERATOR_MAPPING[lookup_type] % cast_sql)
    795800    except KeyError:
    796801        pass
    797802    if lookup_type == 'in':
    798803        in_string = ','.join(['%s' for id in value])
    799804        if in_string:
    800             return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
     805            return '%s IN (%s)' % (field_sql, in_string)
    801806        else:
    802807            raise EmptyResultSet
    803808    elif lookup_type in ('range', 'year'):
    804         return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
     809        return '%s BETWEEN %%s AND %%s' % field_sql
    805810    elif lookup_type in ('month', 'day'):
    806         return "%s = %%s" % backend.get_date_extract_sql(lookup_type, table_prefix + field_name)
     811        return "%s = %%s" % backend.get_date_extract_sql(lookup_type, field_sql)
    807812    elif lookup_type == 'isnull':
    808         return "%s%s IS %sNULL" % (table_prefix, field_name, (not value and 'NOT ' or ''))
     813        return "%s IS %sNULL" % (field_sql, (not value and 'NOT ' or ''))
    809814    elif lookup_type == 'search':
    810         return backend.get_fulltext_search_sql(table_prefix + field_name)
     815        return backend.get_fulltext_search_sql(field_sql)
    811816    elif lookup_type in ('regex', 'iregex'):
    812817        if settings.DATABASE_ENGINE == 'oracle':
    813818            if lookup_type == 'regex':
    814819                match_option = 'c'
    815820            else:
    816821                match_option = 'i'
    817             return "REGEXP_LIKE(%s%s, %s, '%s')" % (table_prefix, field_name, cast_sql, match_option)
     822            return "REGEXP_LIKE(%s, %s, '%s')" % (field_sql, cast_sql, match_option)
    818823        else:
    819824            raise NotImplementedError
    820825    raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type)
     
    10711076    else:
    10721077        # No elements left in path. Current element is the element on which
    10731078        # the search is being performed.
     1079        db_type = None
    10741080
    10751081        if join_required:
    10761082            # Last query term is a RelatedObject
     
    11001106        else:
    11011107            # Last query term was a normal field.
    11021108            column = field.column
     1109            db_type = field.db_type()
    11031110
    1104         where.append(get_where_clause(lookup_type, current_table + '.', column, value))
     1111        where.append(get_where_clause(lookup_type, current_table + '.', column, value, db_type))
    11051112        params.extend(field.get_db_prep_lookup(lookup_type, value))
    11061113
    11071114    return joins, where, params
  • django/db/backends/oracle/base.py

     
    181181def get_datetime_cast_sql():
    182182    return "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')"
    183183
     184def get_field_cast_sql(db_type):
     185    if db_type.endswith('LOB'):
     186        return "DBMS_LOB.SUBSTR(%s%s)"
     187    else:
     188        return "%s%s"
     189
    184190def get_limit_offset_sql(limit, offset=None):
    185191    # Limits and offset are too complicated to be handled here.
    186192    # Instead, they are handled in django/db/backends/oracle/query.py.
  • tests/regressiontests/string_lookup/models.py

     
    3636    def __unicode__(self):
    3737        return "Base %s" % self.name
    3838
     39class Article(models.Model):
     40    name = models.CharField(maxlength = 50)
     41    text = models.TextField()
     42
     43    def __str__(self):
     44        return "Article %s" % self.name
     45
    3946__test__ = {'API_TESTS': ur"""
    4047# Regression test for #1661 and #1662: Check that string form referencing of
    4148# models works, both as pre and post reference, on all RelatedField types.
     
    8289# We can also do the above query using UTF-8 strings.
    8390>>> Foo.objects.get(friend__contains='\xc3\xa7')
    8491<Foo: Foo Bjorn>
     92
     93# Regression tests for #5087: make sure we can perform queries on TextFields.
     94>>> a = Article(name='Test', text='The quick brown fox jumps over the lazy dog.')
     95>>> a.save()
     96>>> Article.objects.get(text__exact='The quick brown fox jumps over the lazy dog.')
     97<Article: Article Test>
     98
     99>>> Article.objects.get(text__contains='quick brown fox')
     100<Article: Article Test>
    85101"""}
Back to Top