Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5087 -- Fixed support for TextField filtering with Oracle. Tha…

…nks, Ian Kelly.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5943 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7e8efcd4d9cd98f0f22be40249959b81c91fed00 1 parent e0c1ca7
Malcolm Tredinnick authored August 19, 2007
6  django/db/backends/oracle/base.py
@@ -181,6 +181,12 @@ def get_date_trunc_sql(lookup_type, field_name):
181 181
 def get_datetime_cast_sql():
182 182
     return "TO_TIMESTAMP(%s, 'YYYY-MM-DD HH24:MI:SS.FF')"
183 183
 
  184
+def 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
+
184 190
 def get_limit_offset_sql(limit, offset=None):
185 191
     # Limits and offset are too complicated to be handled here.
186 192
     # Instead, they are handled in django/db/backends/oracle/query.py.
29  django/db/models/query.py
@@ -777,7 +777,7 @@ def get_sql(self, opts):
777 777
             return SortedDict(), [], []
778 778
         return joins, where2, params
779 779
 
780  
-def get_where_clause(lookup_type, table_prefix, field_name, value):
  780
+def get_where_clause(lookup_type, table_prefix, field_name, value, db_type):
781 781
     if table_prefix.endswith('.'):
782 782
         table_prefix = backend.quote_name(table_prefix[:-1])+'.'
783 783
     field_name = backend.quote_name(field_name)
@@ -785,36 +785,41 @@ def get_where_clause(lookup_type, table_prefix, field_name, value):
785 785
         cast_sql = backend.get_datetime_cast_sql()
786 786
     else:
787 787
         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)
788 793
     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'
790 795
     else:
791  
-        format = '%s%s %s'
  796
+        format = '%s %s'
792 797
     try:
793  
-        return format % (table_prefix, field_name,
  798
+        return format % (field_sql,
794 799
                          backend.OPERATOR_MAPPING[lookup_type] % cast_sql)
795 800
     except KeyError:
796 801
         pass
797 802
     if lookup_type == 'in':
798 803
         in_string = ','.join(['%s' for id in value])
799 804
         if in_string:
800  
-            return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
  805
+            return '%s IN (%s)' % (field_sql, in_string)
801 806
         else:
802 807
             raise EmptyResultSet
803 808
     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
805 810
     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)
807 812
     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 ''))
809 814
     elif lookup_type == 'search':
810  
-        return backend.get_fulltext_search_sql(table_prefix + field_name)
  815
+        return backend.get_fulltext_search_sql(field_sql)
811 816
     elif lookup_type in ('regex', 'iregex'):
812 817
         if settings.DATABASE_ENGINE == 'oracle':
813 818
             if lookup_type == 'regex':
814 819
                 match_option = 'c'
815 820
             else:
816 821
                 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)
818 823
         else:
819 824
             raise NotImplementedError
820 825
     raise TypeError, "Got invalid lookup_type: %s" % repr(lookup_type)
@@ -1071,6 +1076,7 @@ def lookup_inner(path, lookup_type, value, opts, table, column):
1071 1076
     else:
1072 1077
         # No elements left in path. Current element is the element on which
1073 1078
         # the search is being performed.
  1079
+        db_type = None
1074 1080
 
1075 1081
         if join_required:
1076 1082
             # Last query term is a RelatedObject
@@ -1100,8 +1106,9 @@ def lookup_inner(path, lookup_type, value, opts, table, column):
1100 1106
         else:
1101 1107
             # Last query term was a normal field.
1102 1108
             column = field.column
  1109
+            db_type = field.db_type()
1103 1110
 
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))
1105 1112
         params.extend(field.get_db_prep_lookup(lookup_type, value))
1106 1113
 
1107 1114
     return joins, where, params
16  tests/regressiontests/string_lookup/models.py
@@ -36,6 +36,13 @@ class Base(models.Model):
36 36
     def __unicode__(self):
37 37
         return "Base %s" % self.name
38 38
 
  39
+class 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
+
39 46
 __test__ = {'API_TESTS': ur"""
40 47
 # Regression test for #1661 and #1662: Check that string form referencing of
41 48
 # models works, both as pre and post reference, on all RelatedField types.
@@ -82,4 +89,13 @@ def __unicode__(self):
82 89
 # We can also do the above query using UTF-8 strings.
83 90
 >>> Foo.objects.get(friend__contains='\xc3\xa7')
84 91
 <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>
85 101
 """}

0 notes on commit 7e8efcd

Please sign in to comment.
Something went wrong with that request. Please try again.