cqlsh: suggesting cql3 to cql2 users when they get an error and their

statement appears to parse for cql 2, and vice versa.
Patch by paul cannon, reviewed by brandonwilliams for CASSANDRA-4454
This commit is contained in:
Brandon Williams 2012-09-13 14:01:41 -05:00
parent 1e6cc9aebc
commit 5a63858d0d
2 changed files with 27 additions and 0 deletions

View File

@ -1027,6 +1027,18 @@ class Shell(cmd.Cmd):
if trynum > self.num_retries:
return False
time.sleep(1*trynum)
except cql.ProgrammingError, err:
self.printerr(str(err))
# try reparsing as cql3; if successful, suggest -3
if self.cqlver_atleast(3):
if self.parseable_as_cql2(statement):
self.printerr("Perhaps you meant to use CQL 2? Try using"
" the -2 option when starting cqlsh.")
else:
if self.parseable_as_cql3(statement):
self.printerr("Perhaps you meant to use CQL 3? Try using"
" the -3 option when starting cqlsh.")
return False
except CQL_ERRORS, err:
self.printerr(str(err))
return False
@ -1041,6 +1053,17 @@ class Shell(cmd.Cmd):
self.print_result(self.cursor)
return True
# these next two functions are not guaranteed perfect; just checks if the
# statement parses fully according to cqlsh's own understanding of the
# grammar. Changes to the language in Cassandra frequently don't get
# updated in cqlsh right away.
def parseable_as_cql3(self, statement):
return cql3handling.CqlRuleSet.lex_and_whole_match(statement) is not None
def parseable_as_cql2(self, statement):
return cqlhandling.CqlRuleSet.lex_and_whole_match(statement) is not None
def determine_decoder_for(self, cfname, ksname=None):
decoder = ErrorHandlingSchemaDecoder
if ksname is None:

View File

@ -446,6 +446,10 @@ class ParsingRuleSet:
def lex_and_parse(self, text, startsymbol='Start'):
return self.parse(startsymbol, self.lex(text), init_bindings={'*SRC*': text})
def lex_and_whole_match(self, text, startsymbol='Start'):
tokens = self.lex(text)
return self.whole_match(startsymbol, tokens, srcstr=text)
def complete(self, startsymbol, tokens, init_bindings=None):
if init_bindings is None:
init_bindings = {}