'Returns the spatial aggregate SQL template and function for the
given Aggregate instance.'

def spatial_aggregate_sql(self, agg):

    agg_name = agg.__class__.__name__.lower()
    if (agg_name == 'union'):
        agg_name += 'agg'
    if agg.is_extent:
        sql_template = '%(function)s(%(field)s)'
    else:
        sql_template = '%(function)s(SDOAGGRTYPE(%(field)s,%(tolerance)s))'
    sql_function = getattr(self, agg_name)
    return ((self.select % sql_template), sql_function)
