Django

Code

Changeset 7087

Show
Ignore:
Timestamp:
02/04/08 06:02:36 (10 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Moved some backend-specific features into the database
backend code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/backends/__init__.py

    r6690 r7087  
    8787        retrieved as a Python datetime object instead of a string. 
    8888 
    89         This SQL should include a '%s' in place of the field's name. This 
    90         method should return None if no casting is necessary. 
    91         """ 
    92         return None 
     89        This SQL should include a '%s' in place of the field's name. 
     90        """ 
     91        return "%s" 
    9392 
    9493    def deferrable_sql(self): 
     
    170169        return sql 
    171170 
     171    def lookup_cast(self, lookup_type): 
     172        """ 
     173        Returns the string to use in a query when performing lookups 
     174        ("contains", "like", etc). The resulting string should contain a '%s' 
     175        placeholder for the column being searched against. 
     176        """ 
     177        return "%s" 
     178 
    172179    def max_name_length(self): 
    173180        """ 
     
    206213        return 'RANDOM()' 
    207214 
     215    def regex_lookup(self, lookup_type): 
     216        """ 
     217        Returns the string to use in a query when performing regular expression 
     218        lookups (using "regex" or "iregex"). The resulting string should 
     219        contain a '%s' placeholder for the column being searched against. 
     220 
     221        If the feature is not supported (or part of it is not supported), a 
     222        NotImplementedError exception can be raised. 
     223        """ 
     224        raise NotImplementedError 
     225 
    208226    def sql_flush(self, style, tables, sequences): 
    209227        """ 
  • django/branches/queryset-refactor/django/db/backends/oracle/base.py

    r7004 r7087  
    44Requires cx_Oracle: http://www.python.net/crew/atuining/cx_Oracle/ 
    55""" 
     6 
     7import datetime 
     8import os 
    69 
    710from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    811from django.utils.datastructures import SortedDict 
    912from django.utils.encoding import smart_str, force_unicode 
    10 import datetime 
    11 import os 
    1213 
    1314# Oracle takes client-side character set encoding from the environment. 
     
    8990        # Instead, they are handled in django/db/backends/oracle/query.py. 
    9091        return "" 
     92 
     93    def lookup_cast(self, lookup_type): 
     94        if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith'): 
     95            return "UPPER(%s)" 
     96        return "%s" 
    9197 
    9298    def max_name_length(self): 
     
    340346        return "DBMS_RANDOM.RANDOM" 
    341347 
     348    def regex_lookup_9(self, lookup_type): 
     349        raise NotImplementedError("Regexes are not supported in Oracle before version 10g.") 
     350 
     351    def regex_lookup_10(self, lookup_type): 
     352        if lookup_type == 'regex': 
     353            match_option = 'c' 
     354        else: 
     355            match_option = 'i' 
     356        return 'REGEXP_LIKE(%%s %%s %s)' % match_option 
     357 
    342358    def sql_flush(self, style, tables, sequences): 
    343359        # Return a list of 'TRUNCATE x;', 'TRUNCATE y;', 
     
    431447            try: 
    432448                self.oracle_version = int(self.connection.version.split('.')[0]) 
     449                # There's no way for the DatabaseOperations class to know the 
     450                # currently active Oracle version, so we do some setups here. 
     451                # TODO: Multi-db support will need a better solution (a way to 
     452                # communicate the current version). 
     453                if self.oracle_version <= 9: 
     454                    self.ops.regex_lookup = self.ops.regex_lookup_9 
     455                else: 
     456                    self.ops.regex_lookup = self.ops.regex_lookup_10 
    433457            except ValueError: 
    434458                pass 
  • django/branches/queryset-refactor/django/db/models/sql/where.py

    r6967 r7087  
    100100 
    101101        if isinstance(value, datetime.datetime): 
    102             # FIXME datetime_cast_sql() should return '%s' by default. 
    103             cast_sql = connection.ops.datetime_cast_sql() or '%s' 
     102            cast_sql = connection.ops.datetime_cast_sql() 
    104103        else: 
    105104            cast_sql = '%s' 
    106105 
    107         # FIXME: This is out of place. Move to a function like 
    108         # datetime_cast_sql() 
    109         if (lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith') 
    110                 and connection.features.needs_upper_for_iops): 
    111             format = 'UPPER(%s) %s' 
    112         else: 
    113             format = '%s %s' 
    114  
     106        format = "%s %%s" % connection.ops.lookup_cast(lookup_type) 
    115107        params = field.get_db_prep_lookup(lookup_type, value) 
    116108 
     
    136128            return (connection.ops.fulltest_search_sql(field_sql), params) 
    137129        elif lookup_type in ('regex', 'iregex'): 
    138             # FIXME: Factor this out in to connection.ops 
    139             if settings.DATABASE_ENGINE == 'oracle': 
    140                 if connection.oracle_version and connection.oracle_version <= 9: 
    141                     raise NotImplementedError("Regexes are not supported in Oracle before version 10g.") 
    142                 if lookup_type == 'regex': 
    143                     match_option = 'c' 
    144                 else: 
    145                     match_option = 'i' 
    146                 return ("REGEXP_LIKE(%s, %s, '%s')" % (field_sql, cast_sql, 
    147                         match_option), params) 
    148             else: 
    149                 raise NotImplementedError 
     130            return connection.ops.regex_lookup % (field_sql, cast_sql), params 
    150131 
    151132        raise TypeError('Invalid lookup_type: %r' % lookup_type)