Exemplo n.º 1
0
def suggest_special(text):
    text = text.lstrip()
    cmd, _, arg = parse_special_command(text)

    if cmd == text:
        # Trying to complete the special command itself
        return (Special(), )

    if cmd in ('\\c', '\\connect'):
        return (Database(), )

    if cmd == '\\T':
        return (TableFormat(), )

    if cmd == '\\dn':
        return (Schema(), )

    if arg:
        # Try to distinguish "\d name" from "\d schema.name"
        # Note that this will fail to obtain a schema name if wildcards are
        # used, e.g. "\d schema???.name"
        parsed = sqlparse.parse(arg)[0].tokens[0]
        try:
            schema = parsed.get_parent_name()
        except AttributeError:
            schema = None
    else:
        schema = None

    if cmd[1:] == 'd':
        # \d can describe tables or views
        if schema:
            return (
                Table(schema=schema),
                View(schema=schema),
            )
        else:
            return (
                Schema(),
                Table(schema=None),
                View(schema=None),
            )
    elif cmd[1:] in SPECIALS_SUGGESTION:
        rel_type = SPECIALS_SUGGESTION[cmd[1:]]
        if schema:
            if rel_type == Function:
                return (Function(schema=schema, usage='special'), )
            return (rel_type(schema=schema), )
        else:
            if rel_type == Function:
                return (
                    Schema(),
                    Function(schema=None, usage='special'),
                )
            return (Schema(), rel_type(schema=None))

    if cmd in ['\\n', '\\ns', '\\nd']:
        return (NamedQuery(), )

    return (Keyword(), Special())
Exemplo n.º 2
0
def suggest_special(text):
    text = text.lstrip()
    cmd, _, arg = parse_special_command(text)

    if cmd == text:
        # Trying to complete the special command itself
        return (Special(),)

    if cmd in ('\\c', '\\connect'):
        return (Database(),)

    if cmd == '\\T':
        return (TableFormat(),)

    if cmd == '\\dn':
        return (Schema(),)

    if arg:
        # Try to distinguish "\d name" from "\d schema.name"
        # Note that this will fail to obtain a schema name if wildcards are
        # used, e.g. "\d schema???.name"
        parsed = sqlparse.parse(arg)[0].tokens[0]
        try:
            schema = parsed.get_parent_name()
        except AttributeError:
            schema = None
    else:
        schema = None

    if cmd[1:] == 'd':
        # \d can describe tables or views
        if schema:
            return (Table(schema=schema),
                    View(schema=schema),)
        else:
            return (Schema(),
                    Table(schema=None),
                    View(schema=None),)
    elif cmd[1:] in SPECIALS_SUGGESTION:
        rel_type = SPECIALS_SUGGESTION[cmd[1:]]
        if schema:
            if rel_type == Function:
                return (Function(schema=schema, usage='special'),)
            return (rel_type(schema=schema),)
        else:
            if rel_type == Function:
                return (Schema(), Function(schema=None, usage='special'),)
            return (Schema(), rel_type(schema=None))

    if cmd in ['\\n', '\\ns', '\\nd']:
        return (NamedQuery(),)

    return (Keyword(), Special())
Exemplo n.º 3
0
def suggest_special(text):
    text = text.lstrip()
    cmd, _, arg = parse_special_command(text)

    if cmd == text:
        # Trying to complete the special command itself
        return [{'type': 'special'}]

    if cmd in ('\\c', '\\connect'):
        return [{'type': 'database'}]

    if cmd == '\\dn':
        return [{'type': 'schema'}]

    if arg:
        # Try to distinguish "\d name" from "\d schema.name"
        # Note that this will fail to obtain a schema name if wildcards are
        # used, e.g. "\d schema???.name"
        parsed = sqlparse.parse(arg)[0].tokens[0]
        try:
            schema = parsed.get_parent_name()
        except AttributeError:
            schema = None
    else:
        schema = None

    if cmd[1:] == 'd':
        # \d can descibe tables or views
        if schema:
            return [{'type': 'table', 'schema': schema},
                    {'type': 'view', 'schema': schema}]
        else:
            return [{'type': 'schema'},
                    {'type': 'table', 'schema': []},
                    {'type': 'view', 'schema': []}]
    elif cmd[1:] in ('dt', 'dv', 'df', 'dT'):
        rel_type = {'dt': 'table',
                    'dv': 'view',
                    'df': 'function',
                    'dT': 'datatype',
                    }[cmd[1:]]
        if schema:
            return [{'type': rel_type, 'schema': schema}]
        else:
            return [{'type': 'schema'},
                    {'type': rel_type, 'schema': []}]

    if cmd in ['\\n', '\\ns', '\\nd']:
        return [{'type': 'namedquery'}]

    return [{'type': 'keyword'}, {'type': 'special'}]