Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocompletion for role names/user names #3766

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions pylib/cqlshlib/cql3handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,9 +1500,9 @@ def alter_type_field_completer(ctxt, cass):
'''

syntax_rules += r'''
<rolename> ::= <identifier>
<rolename> ::= role=( <identifier>
| <quotedName>
| <unreservedKeyword>
| <unreservedKeyword> )
;

<createRoleStatement> ::= "CREATE" "ROLE" ("IF" "NOT" "EXISTS")? <rolename>
Expand Down Expand Up @@ -1611,32 +1611,23 @@ def permission_completer(ctxt, _):

@completer_for('username', 'name')
def username_name_completer(ctxt, cass):
def maybe_quote(name):
if CqlRuleSet.is_valid_cql3_name(name):
return name
return "'%s'" % name

# disable completion for CREATE USER.
if ctxt.matched[0][1].upper() == 'CREATE':
return [Hint('<username>')]

session = cass.session
return [maybe_quote(list(row.values())[0].replace("'", "''")) for row in session.execute("LIST USERS")]
return map(maybe_escape_name, [row['name'] for row in session.execute("LIST USERS")])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protect_name internally calls maybe_escape_name. Maybe I'm missing something, but what would be the benefit of using protect_name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, just use the "higher abstraction" provided in the driver? It is just a nit, but I am curious to why protect_name even exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me it feels like it's more of a legacy method name : datastax/python-driver@0959b5d and not really a higher abstraction. But I don't have a lot of context on the history of these method names




@completer_for('rolename', 'role')
def rolename_completer(ctxt, cass):
def maybe_quote(name):
if CqlRuleSet.is_valid_cql3_name(name):
return name
return "'%s'" % name

# disable completion for CREATE ROLE.
if ctxt.matched[0][1].upper() == 'CREATE':
return [Hint('<rolename>')]

session = cass.session
return [maybe_quote(row[0].replace("'", "''")) for row in session.execute("LIST ROLES")]
return map(maybe_escape_name, [row['role'] for row in session.execute("LIST ROLES")])


syntax_rules += r'''
Expand Down
2 changes: 1 addition & 1 deletion pylib/cqlshlib/test/test_cqlsh_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ def test_complete_in_alter_user(self):
self.trycompletions('ALTER USER ', choices=['<identifier>', 'IF', '<pgStringLiteral>', '<quotedStringLiteral>'])

def test_complete_in_create_role(self):
self.trycompletions('CREATE ROLE ', choices=['<identifier>', 'IF', '<quotedName>'])
self.trycompletions('CREATE ROLE ', choices=['<rolename>', 'IF'])
self.trycompletions('CREATE ROLE IF ', immediate='NOT EXISTS ')
self.trycompletions('CREATE ROLE foo WITH ', choices=['ACCESS', 'HASHED', 'LOGIN', 'OPTIONS', 'PASSWORD', 'SUPERUSER', 'GENERATED'])
self.trycompletions('CREATE ROLE foo WITH HASHED ', immediate='PASSWORD = ')
Expand Down