import re


def glob2like(s):
    """ Convert a file glob pattern to an equivalent SQL ``LIKE`` pattern """
    def subber(m):
        x = m.group(1)
        if x == '*':
            return '%'
        elif x == '?':
            return '_'
        elif x in (r'\*', r'\?'):
            return x[-1]
        elif x in (r'\%', r'\_'):
            return r'\\' + x
        elif x == r'\\':
            return x
        else:
            return '\\' + x
    return re.sub(r'(\x5C.|[?*%_])', subber, s)