| 1 |
from types import UnicodeType |
|---|
| 2 |
|
|---|
| 3 |
def gqn(val): |
|---|
| 4 |
""" |
|---|
| 5 |
The geographic quote name function; used for quoting tables and |
|---|
| 6 |
geometries (they use single rather than the double quotes of the |
|---|
| 7 |
backend quotename function). |
|---|
| 8 |
""" |
|---|
| 9 |
if isinstance(val, basestring): |
|---|
| 10 |
if isinstance(val, UnicodeType): val = val.encode('ascii') |
|---|
| 11 |
return "'%s'" % val |
|---|
| 12 |
else: |
|---|
| 13 |
return str(val) |
|---|
| 14 |
|
|---|
| 15 |
class SpatialOperation(object): |
|---|
| 16 |
""" |
|---|
| 17 |
Base class for generating spatial SQL. |
|---|
| 18 |
""" |
|---|
| 19 |
def __init__(self, function='', operator='', result='', beg_subst='', end_subst=''): |
|---|
| 20 |
self.function = function |
|---|
| 21 |
self.operator = operator |
|---|
| 22 |
self.result = result |
|---|
| 23 |
self.beg_subst = beg_subst |
|---|
| 24 |
try: |
|---|
| 25 |
# Try and put the operator and result into to the |
|---|
| 26 |
# end substitution. |
|---|
| 27 |
self.end_subst = end_subst % (operator, result) |
|---|
| 28 |
except TypeError: |
|---|
| 29 |
self.end_subst = end_subst |
|---|
| 30 |
|
|---|
| 31 |
@property |
|---|
| 32 |
def sql_subst(self): |
|---|
| 33 |
return ''.join([self.beg_subst, self.end_subst]) |
|---|
| 34 |
|
|---|
| 35 |
def as_sql(self, geo_col): |
|---|
| 36 |
return self.sql_subst % self.params(geo_col) |
|---|
| 37 |
|
|---|
| 38 |
def params(self, geo_col): |
|---|
| 39 |
return (geo_col, self.operator) |
|---|
| 40 |
|
|---|
| 41 |
class SpatialFunction(SpatialOperation): |
|---|
| 42 |
""" |
|---|
| 43 |
Base class for generating spatial SQL related to a function. |
|---|
| 44 |
""" |
|---|
| 45 |
def __init__(self, func, beg_subst='%s(%s, %%s', end_subst=')', result='', operator=''): |
|---|
| 46 |
# Getting the function prefix. |
|---|
| 47 |
kwargs = {'function' : func, 'operator' : operator, 'result' : result, |
|---|
| 48 |
'beg_subst' : beg_subst, 'end_subst' : end_subst,} |
|---|
| 49 |
super(SpatialFunction, self).__init__(**kwargs) |
|---|
| 50 |
|
|---|
| 51 |
def params(self, geo_col): |
|---|
| 52 |
return (self.function, geo_col) |
|---|