comparison sqlpyPlus.py @ 116:6e346ae994b9

beginning adaptation to new cmd2
author catherine@Elli.myhome.westell.com
date Fri, 04 Jul 2008 14:54:01 -0400
parents 289b0a472b65
children dfb71885dd41
comparison
equal deleted inserted replaced
111:289b0a472b65 116:6e346ae994b9
503 return result 503 return result
504 504
505 statementEndPattern = re.compile(r'(.*)(;|\\[gGhtxicCsS])\s*(\d*)\s*$', re.DOTALL | re.MULTILINE) 505 statementEndPattern = re.compile(r'(.*)(;|\\[gGhtxicCsS])\s*(\d*)\s*$', re.DOTALL | re.MULTILINE)
506 # what about quote-enclosed? 506 # what about quote-enclosed?
507 507
508 def findTerminator(self, statement):
509 m = self.statementEndPattern.search(statement)
510 if m:
511 return m.groups()
512 else:
513 return statement, None, None
514
515 legalOracle = re.compile('[a-zA-Z_$#]') 508 legalOracle = re.compile('[a-zA-Z_$#]')
516 509
510 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit')
511 terminators = pyparsing.oneOf('; \\s \\S \\c \\C \\t \\x \\h \n/\n')('terminator') + \
512 pyparsing.Optional(rowlimitPattern)
517 def do_select(self, arg, bindVarsIn=None, override_terminator=None): 513 def do_select(self, arg, bindVarsIn=None, override_terminator=None):
518 """Fetch rows from a table. 514 """Fetch rows from a table.
519 515
520 Limit the number of rows retrieved by appending 516 Limit the number of rows retrieved by appending
521 an integer after the terminator 517 an integer after the terminator
523 519
524 Output may be formatted by choosing an alternative terminator 520 Output may be formatted by choosing an alternative terminator
525 ("help terminators" for details) 521 ("help terminators" for details)
526 """ 522 """
527 bindVarsIn = bindVarsIn or {} 523 bindVarsIn = bindVarsIn or {}
528 self.query = 'select ' + arg 524 statement = self.parsed('select ' + arg)
529 (self.query, terminator, rowlimit) = self.findTerminator(self.query) 525 self.query = statement.statement
530 if override_terminator: 526 if override_terminator:
531 terminator = override_terminator 527 statement['terminator'] = override_terminator
532 rowlimit = int(rowlimit or 0) 528 statement['rowlimit'] = int(statement.rowlimit or 0)
533 try: 529 try:
534 self.varsUsed = findBinds(self.query, self.binds, bindVarsIn) 530 self.varsUsed = findBinds(self.query, self.binds, bindVarsIn)
535 self.curs.execute(self.query, self.varsUsed) 531 self.curs.execute(self.query, self.varsUsed)
536 self.rows = self.curs.fetchmany(min(self.maxfetch, (rowlimit or self.maxfetch))) 532 self.rows = self.curs.fetchmany(min(self.maxfetch, (statement.rowlimit or self.maxfetch)))
537 self.desc = self.curs.description 533 self.desc = self.curs.description
538 self.rc = self.curs.rowcount 534 self.rc = self.curs.rowcount
539 if self.rc > 0: 535 if self.rc > 0:
540 self.stdout.write('\n%s\n' % (self.output(terminator, rowlimit))) 536 self.stdout.write('\n%s\n' % (self.output(statement.terminator, rowlimit)))
541 if self.rc == 0: 537 if self.rc == 0:
542 print '\nNo rows Selected.\n' 538 print '\nNo rows Selected.\n'
543 elif self.rc == 1: 539 elif self.rc == 1:
544 print '\n1 row selected.\n' 540 print '\n1 row selected.\n'
545 if self.autobind: 541 if self.autobind: