Django

Code

Show
Ignore:
Timestamp:
12/14/07 18:30:48 (1 year ago)
Author:
jbronn
Message:

gis: Applied DRY to spatial SQL generation in anticipation of queryset-refactor; fixed gml function for PostGIS 1.3.2 parameter ordering.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/db/backend/util.py

    r6886 r6919  
    99 
    1010    def __str__(self): 
    11         return self.where[0] % tuple(self.params
     11        return self.as_sql(
    1212 
    13 def get_srid(field, geom): 
     13    def as_sql(self, quote=False): 
     14        if not quote: 
     15            return self.where[0] % tuple(self.params) 
     16        else: 
     17            # Used for quoting WKT on certain backends. 
     18            tmp_params = ["'%s'" % self.params[0]] 
     19            tmp_params.extend(self.params[1:]) 
     20            return self.where[0] % tuple(tmp_params) 
     21 
     22class SpatialOperation(object): 
    1423    """ 
    15     Gets the SRID depending on the value of the SRID setting of the field 
    16     and that of the given geometry. 
     24    Base class for generating spatial SQL. 
    1725    """ 
    18     if geom.srid is None or (geom.srid == -1 and field._srid != -1): 
    19         return field._srid 
    20     else: 
    21         return geom.srid 
     26    def __init__(self, function='', operator='', result='', beg_subst='', end_subst=''): 
     27        self.function = function 
     28        self.operator = operator 
     29        self.result = result 
     30        self.beg_subst = beg_subst 
     31        try: 
     32            # Try and put the operator and result into to the 
     33            # end substitution. 
     34            self.end_subst = end_subst % (operator, result) 
     35        except TypeError: 
     36            self.end_subst = end_subst 
     37 
     38    @property 
     39    def sql_subst(self): 
     40        return ''.join([self.beg_subst, self.end_subst]) 
     41 
     42    def as_sql(self, geo_col): 
     43        return self.sql_subst % self.params(geo_col) 
     44 
     45    def params(self, geo_col): 
     46        return (geo_col, self.operator) 
     47 
     48class SpatialFunction(SpatialOperation): 
     49    """ 
     50    Base class for generating spatial SQL related to a function. 
     51    """ 
     52    def __init__(self, func, beg_subst='%s(%s, %%s', end_subst=')', result='', operator=''): 
     53        # Getting the function prefix. 
     54        kwargs = {'function' : func, 'operator' : operator, 'result' : result, 
     55                  'beg_subst' : beg_subst, 'end_subst' : end_subst,} 
     56        super(SpatialFunction, self).__init__(**kwargs) 
     57 
     58    def params(self, geo_col): 
     59        return (self.function, geo_col)