comparison sqlpyPlus.py @ 131:2b7ce838120d

experimenting with completion
author catherine@Elli.myhome.westell.com
date Wed, 27 Aug 2008 19:35:28 -0400
parents 40bbe808e98a
children 2baecb3d5356
comparison
equal deleted inserted replaced
130:40bbe808e98a 131:2b7ce838120d
321 else: 321 else:
322 if sqlcommand[0]: 322 if sqlcommand[0]:
323 itms.append(sqlcommand[0]) 323 itms.append(sqlcommand[0])
324 return itms 324 return itms
325 325
326 bindScanner = Parser(pyparsing.Literal(':') + pyparsing.Word( pyparsing.alphanums + "_$#" )) 326 bindScanner = Parser(pyparsing.Literal(':') + pyparsing.Word( pyparsing.alphanums + "_$#" ))
327 327
328 def findBinds(target, existingBinds, givenBindVars = {}): 328 def findBinds(target, existingBinds, givenBindVars = {}):
329 result = givenBindVars 329 result = givenBindVars
330 for finding, startat, endat in bindScanner.scanner.scanString(target): 330 for finding, startat, endat in bindScanner.scanner.scanString(target):
331 varname = finding[1] 331 varname = finding[1]
332 try: 332 try:
504 result = sqlpython.pmatrix(self.rows, self.curs.description, self.maxfetch) 504 result = sqlpython.pmatrix(self.rows, self.curs.description, self.maxfetch)
505 return result 505 return result
506 506
507 legalOracle = re.compile('[a-zA-Z_$#]') 507 legalOracle = re.compile('[a-zA-Z_$#]')
508 508
509 def complete_select(self, text, line, begidx, endidx):
510 completions = []
511 for statement in """SELECT column_name FROM all_tab_columns WHERE column_name LIKE '%s%%'
512 SELECT table_name FROM all_tables WHERE table_name LIKE '%s%%'
513 SELECT DISTINCT owner FROM all_tables WHERE owner LIKE '%%%s'""".splitlines():
514 self.curs.execute(statement % text.upper())
515 completions.extend([r[0] for r in self.curs.fetchall()])
516 return completions
517
509 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit') 518 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit')
510 terminatorPattern = (pyparsing.oneOf('; \\s \\S \\c \\C \\t \\x \\h') 519 terminatorPattern = (pyparsing.oneOf('; \\s \\S \\c \\C \\t \\x \\h')
511 ^ pyparsing.Literal('\n/') ^ \ 520 ^ pyparsing.Literal('\n/') ^ \
512 (pyparsing.Literal('\nEOF') + pyparsing.stringEnd)) \ 521 (pyparsing.Literal('\nEOF') + pyparsing.stringEnd)) \
513 ('terminator') + \ 522 ('terminator') + \
552 print e 561 print e
553 import traceback 562 import traceback
554 traceback.print_exc(file=sys.stdout) 563 traceback.print_exc(file=sys.stdout)
555 self.sqlBuffer.append(self.query) 564 self.sqlBuffer.append(self.query)
556 565
566
557 @options([make_option('-f', '--full', action='store_true', help='get dependent objects as well')]) 567 @options([make_option('-f', '--full', action='store_true', help='get dependent objects as well')])
558 def do_pull(self, arg, opts): 568 def do_pull(self, arg, opts):
559 """Displays source code.""" 569 """Displays source code."""
560 570
561 arg = self.parsed(arg).unterminated 571 arg = self.parsed(arg).unterminated
966 arg = self.parsed(arg).unterminated.upper() 976 arg = self.parsed(arg).unterminated.upper()
967 object_type, owner, object_name = self.resolve(arg) 977 object_type, owner, object_name = self.resolve(arg)
968 if object_type == 'TABLE': 978 if object_type == 'TABLE':
969 self.do_select(queries['refs'],bindVarsIn={'object_name':object_name, 'owner':owner}) 979 self.do_select(queries['refs'],bindVarsIn={'object_name':object_name, 'owner':owner})
970 980
981 sqlStyleComment = pyparsing.Literal("--") + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))
982 keywords = {'order by': pyparsing.Keyword('order', caseless=True) +
983 pyparsing.Keyword('by', caseless=True),
984 'select': pyparsing.Keyword('select', caseless=True),
985 'from': pyparsing.Keyword('from', caseless=True),
986 'having': pyparsing.Keyword('having', caseless=True),
987 'update': pyparsing.Keyword('update', caseless=True),
988 'set': pyparsing.Keyword('set', caseless=True),
989 'delete': pyparsing.Keyword('delete', caseless=True),
990 'insert into': pyparsing.Keyword('insert', caseless=True) +
991 pyparsing.Keyword('into', caseless=True),
992 'values': pyparsing.Keyword('values', caseless=True),
993 'group by': pyparsing.Keyword('group', caseless=True) +
994 pyparsing.Keyword('by', caseless=True),
995 'where': pyparsing.Keyword('where', caseless=True)}
996 for (name, parser) in keywords.items():
997 parser.ignore(pyparsing.sglQuotedString)
998 parser.ignore(pyparsing.dblQuotedString)
999 parser.ignore(pyparsing.cStyleComment)
1000 parser.ignore(sqlStyleComment)
1001 parser.name = name
1002
1003 def orderedParseResults(parsers, statement):
1004 results = []
1005 for parser in parsers:
1006 results.extend(parser.scanString(statement))
1007 results.sort(cmp=lambda x,y:cmp(x[1],y[1]))
1008 return results
1009
1010 def whichSqlPhrase(statement):
1011 results = orderedParseResults(keywords.values(), statement)
1012 if results:
1013 return ' '.join(results[-1][0])
1014 else:
1015 return None
1016
1017 oracleIdentifierCharacters = pyparsing.alphanums + '_#$'
1018 def wordInProgress(statement):
1019 result = []
1020 letters = list(statement)
1021 letters.reverse()
1022 for letter in letters:
1023 if letter not in oracleIdentifierCharacters:
1024 result.reverse()
1025 return ''.join(result)
1026 result.append(letter)
1027 result.reverse()
1028 return ''.join(result)
1029
971 def _test(): 1030 def _test():
972 import doctest 1031 import doctest
973 doctest.testmod() 1032 doctest.testmod()
974 1033
975 if __name__ == "__main__": 1034 if __name__ == "__main__":