changeset 138:3b3c78bad48f

need completion too
author catherine@Elli.myhome.westell.com
date Fri, 29 Aug 2008 09:54:12 -0400
parents c27eeeea8752
children 0b62e81b068c
files completion.py sqlpyPlus.py
diffstat 2 files changed, 64 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
--- 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%%'"