'Returns the SpatiaLite-specific SQL for the given lookup value
[a tuple of (alias, column, db_type)], lookup type, lookup
value, the model field, and the quoting function.'

def spatial_lookup_sql(self, lvalue, lookup_type, value, field, qn):

    (alias, col, db_type) = lvalue
    geo_col = ('%s.%s' % (qn(alias), qn(col)))
    if (lookup_type in self.geometry_functions):
        tmp = self.geometry_functions[lookup_type]
        if isinstance(tmp, tuple):
            (op, arg_type) = tmp
            if (not isinstance(value, (tuple, list))):
                raise ValueError(('Tuple    required    for    `%s`    lookup    type.' % lookup_type))
            geom = value[0]
            if (len(value) != 2):
                raise ValueError(('Incorrect    number    of    parameters    given    for    `%s`    lookup    type.' % lookup_type))
            if (not isinstance(value[1], arg_type)):
                raise ValueError(('Argument    type    should    be    %s,    got    %s    instead.' % (arg_type, type(value[1]))))
            if (lookup_type == 'relate'):
                op = op(value[1])
            elif (lookup_type in self.distance_functions):
                op = op[0]
        else:
            op = tmp
            geom = value
        return op.as_sql(geo_col, self.get_geom_placeholder(field, geom))
    elif (lookup_type == 'isnull'):
        return (('%s    IS    %sNULL' % (geo_col, ('' if value else 'NOT    '))), [])
    raise TypeError(('Got    invalid    lookup_type:    %s' % repr(lookup_type)))
