'Resolve BELONGS into a DAL expression (or S3ResourceQuery if
field is an S3FieldSelector)
@param l: the left operand
@param r: the right operand
@param field: alternative left operand'

@staticmethod
def _query_belongs(l, r, field=None):

    if (field is None):
        field = l
    expr = None
    none = False
    if (not isinstance(r, (list, tuple, set))):
        items = [r]
    else:
        items = r
    if (None in items):
        none = True
        items = [item for item in items if (item is not None)]
    wildcard = False
    if (str(l.type) in ('string', 'text')):
        for item in items:
            if isinstance(item, basestring):
                if (('*' in item) and ('%' not in item)):
                    s = item.replace('*', '%')
                else:
                    s = item
            else:
                try:
                    s = str(item)
                except:
                    continue
            if ('%' in s):
                wildcard = True
                _expr = field.like(s)
            else:
                _expr = (field == s)
            if (expr is None):
                expr = _expr
            else:
                expr |= _expr
    if (not wildcard):
        if (len(items) == 1):
            expr = (field == tuple(items)[0])
        elif items:
            expr = field.belongs(items)
    if none:
        if (expr is None):
            expr = (field == None)
        else:
            expr |= (field == None)
    elif (expr is None):
        expr = field.belongs(set())
    return expr
