# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1220018052 14400 # Node ID 3b3c78bad48fdb9778ea74057ecae1e19ba767cf # Parent c27eeeea8752f0d14883c55bad910576b85bb819 need completion too diff -r c27eeeea8752 -r 3b3c78bad48f completion.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/completion.py Fri Aug 29 09:54:12 2008 -0400 @@ -0,0 +1,62 @@ +import pyparsing, re + +sqlStyleComment = pyparsing.Literal("--") + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n")) +keywords = {'order by': pyparsing.Keyword('order', caseless=True) + + pyparsing.Keyword('by', caseless=True), + 'select': pyparsing.Keyword('select', caseless=True), + 'from': pyparsing.Keyword('from', caseless=True), + 'having': pyparsing.Keyword('having', caseless=True), + 'update': pyparsing.Keyword('update', caseless=True), + 'set': pyparsing.Keyword('set', caseless=True), + 'delete': pyparsing.Keyword('delete', caseless=True), + 'insert into': pyparsing.Keyword('insert', caseless=True) + + pyparsing.Keyword('into', caseless=True), + 'values': pyparsing.Keyword('values', caseless=True), + 'group by': pyparsing.Keyword('group', caseless=True) + + pyparsing.Keyword('by', caseless=True), + 'where': pyparsing.Keyword('where', caseless=True)} +for (name, parser) in keywords.items(): + parser.ignore(pyparsing.sglQuotedString) + parser.ignore(pyparsing.dblQuotedString) + parser.ignore(pyparsing.cStyleComment) + parser.ignore(sqlStyleComment) + parser.name = name + +fromClauseFinder = re.compile(r".*(from|update)(.*)(where|set)", + re.IGNORECASE | re.DOTALL | re.MULTILINE) +oracleTerms = oracleTerms = re.compile(r"[A-Z$_#][0-9A-Z_$#]*", re.IGNORECASE) +def tableNamesFromFromClause(statement): + result = fromClauseFinder.search(statement) + if not result: + return [] + result = oracleTerms.findall(result.group(2)) + result = [r.upper() for r in result if r.upper() not in ('JOIN','ON')] + return result + +def orderedParseResults(parsers, statement): + results = [] + for parser in parsers: + results.extend(parser.scanString(statement)) + results.sort(cmp=lambda x,y:cmp(x[1],y[1])) + return results + +def whichSegment(statement): + results = orderedParseResults(keywords.values(), statement) + if results: + return ' '.join(results[-1][0]) + else: + return None + +oracleIdentifierCharacters = pyparsing.alphanums + '_#$' +def wordInProgress(statement): + result = [] + letters = list(statement) + letters.reverse() + for letter in letters: + if letter not in oracleIdentifierCharacters: + result.reverse() + return ''.join(result) + result.append(letter) + result.reverse() + return ''.join(result) + \ No newline at end of file diff -r c27eeeea8752 -r 3b3c78bad48f sqlpyPlus.py --- a/sqlpyPlus.py Thu Aug 28 08:27:52 2008 -0400 +++ b/sqlpyPlus.py Fri Aug 29 09:54:12 2008 -0400 @@ -12,6 +12,7 @@ - comments shows table and column comments - compare ... to ... graphically compares results of two queries - commands are case-insensitive + - context-sensitive tab-completion for table names, column names, etc. Use 'help' within sqlpython for details. @@ -530,6 +531,7 @@ columnNames = [c.strip().upper() for c in columnNames.split(',')] stmt1 = "SELECT table_name FROM all_tab_columns WHERE column_name = '%s' AND table_name LIKE '%s%%'" for columnName in columnNames: + # and if columnName is * ? completions.extend(self.select_scalar_list(stmt1 % (columnName, text))) if segment in ('from', 'update', 'insert into') and (not completions): stmt = "SELECT table_name FROM user_tables WHERE table_name LIKE '%s%%'"