changeset 1:8fa146b9a2d7

reworking multiline
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Fri, 30 Nov 2007 16:44:26 -0500
parents 9c87fa772ec1
children 59903dcaf327
files mysqlpy.py sqlpyPlus.py sqlpython.py
diffstat 3 files changed, 40 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mysqlpy.py	Fri Nov 30 13:04:51 2007 -0500
+++ b/mysqlpy.py	Fri Nov 30 16:44:26 2007 -0500
@@ -138,6 +138,7 @@
                 print '\nSelected Max Num rows (%d)' % self.rc                 
         except Exception, e:
             print e
+            
 
     def do_sql(self,args):
         '''prints sql statement give the sql_id (Oracle 10gR2)'''
--- a/sqlpyPlus.py	Fri Nov 30 13:04:51 2007 -0500
+++ b/sqlpyPlus.py	Fri Nov 30 16:44:26 2007 -0500
@@ -266,11 +266,13 @@
         sqlpython.sqlpython.__init__(self)
         self.binds = CaselessDict()
         self.sqlBuffer = []
+        self.history = []
         self.settable = ['maxtselctrows', 'maxfetch', 'autobind', 'failover', 'timeout'] # settables must be lowercase
         self.stdoutBeforeSpool = sys.stdout
         self.spoolFile = None
         self.autobind = False
         self.failover = False
+        self.singleline = '''desc describe'''.split()
 
     def default(self, arg, do_everywhere=False):
         sqlpython.sqlpython.default(self, arg, do_everywhere)
@@ -305,6 +307,7 @@
         interpreted, but after the input prompt is generated and issued.
         Makes commands case-insensitive (but unfortunately does not alter command completion).
         """
+        '''
         savestdout = sys.stdout
         pipefilename = 'sqlpython.pipeline.tmp'
         pipedCommands = pipeSeparator.separate(line)
@@ -317,13 +320,17 @@
             f.close()
             sys.stdout = savestdout
             os.system('%s < %s' % (pipedCommands[1], pipefilename))
+        '''
         try:
             args = line.split(None,1)
             args[0] = args[0].lower()
-            return ' '.join(args)
+            statement = ' '.join(args)            
+            if args[0] not in self.singleline:
+                statement = finishStatement(statement)
+            return statement
         except Exception:
             return line
-
+    
     def do_shortcuts(self,arg):
         """Lists available first-character shortcuts
         (i.e. '!dir' is equivalent to 'shell dir')"""
@@ -364,15 +371,17 @@
     def output_as_html_table(self):
         result = ''.join('<th>%s</th>' % c for c in self.colnames)
         result = ['  <tr>\n    %s\n  </tr>' % result]
+        print result
+        print type(result)
         for row in self.rows:
             result.append('  <tr>\n    %s\n  </tr>' %
                           (''.join('<td>%s</td>' %
                                    str('' if (itm is None) else itm)
                            for itm in row)))                
-            result = '''<table id="%s">
+        result = '''<table id="%s">
 %s
 </table>''' % (self.tblname, '\n'.join(result))
-        return '\n'.join(result)
+        return result
 
     def output_as_list(self, align):
         result = []
@@ -421,10 +430,12 @@
         ("help terminators" for details)
         """
         bindVarsIn = bindVarsIn or {}
-        stmt = sqlpython.Statement('select '+arg)
-        self.query = stmt.query
-        if stmt.outformat == '\\t':
-            self.do_tselect(' '.join(self.query.split()[1:]) + ';', stmt.rowlimit)
+        self.query = 'select ' + arg
+        (self.query, terminator, rowlimit) = sqlpython.findTerminator(self.query)
+        rowlimit = int(rowlimit or 0)
+        if terminator == '\\t':
+            self.do_tselect(' '.join(self.query.split()[1:]) + ';', rowlimit)
+            return
         else:
             try:
                 self.varsUsed = findBinds(self.query, self.binds, bindVarsIn)
@@ -433,7 +444,7 @@
                 self.desc = self.curs.description
                 self.rc = self.curs.rowcount
                 if self.rc > 0:
-                    print '\n' + self.output(stmt.outformat, stmt.rowlimit)
+                    print '\n' + self.output(outformat, rowlimit)
                 if self.rc == 0:
                     print '\nNo rows Selected.\n'
                 elif self.rc == 1: 
@@ -627,6 +638,10 @@
         'run [N]: runs the SQL that was run N commands ago'	
         for pos in self.bufferPositions(arg):
             self.onecmd(self.sqlBuffer[-1-pos])
+    def do_history(self, arg):
+        for (i, itm) in enumerate(self.history):
+            print '-------------------------[%d]' % (i+1)
+            print itm
     def do_list(self, arg):
         'list [N]: lists the SQL that was run N commands ago'
         for pos in self.bufferPositions(arg):
--- a/sqlpython.py	Fri Nov 30 13:04:51 2007 -0500
+++ b/sqlpython.py	Fri Nov 30 16:44:26 2007 -0500
@@ -120,24 +120,21 @@
     do_q = do_quit
     do_exit = do_quit
 
-class Statement(object):
-    prompt2 = ' > '
-    stmtEndSearchString = r'(.*)(%s)\s*(\d+)?\s*$' % sqlpython.terminatorSearchString
-    stmtEnd = re.compile(stmtEndSearchString, re.MULTILINE | re.DOTALL)
-    def __init__(self, firstline):
-        v_Lines = []
-        v_Line = firstline
-        while 1:
-            m = self.stmtEnd.search(v_Line)
-            if m:
-                v_Line, self.outformat, suffix = m.groups()
-                v_Lines.append(v_Line) 
-                self.query = '\n'.join(v_Lines)
-                self.rowlimit = int(suffix or 0)
-                return
-            v_Lines.append(v_Line)
-            v_Line = raw_input(self.prompt2)
-        self.query = '\n'.join(v_Lines)
+stmtEndSearchString = r'(.*)(%s)\s*(\d+)?\s*$' % terminatorSearchString
+stmtEndFinder = re.compile(stmtEndSearchString, re.MULTILINE | re.DOTALL)
+prompt2 = ' > '
+
+def finishStatement(firstline):
+    lines = [firstline]
+    while 1:
+        m = stmtEndFinder.search(lines[-1])
+        if m:
+            return '\n'.join(lines)
+        lines.append(raw_input(prompt2))
+
+def findTerminator(statement):
+    m = stmtEndFinder.search(statement)
+    return m.groups()
     
 def pmatrix(rows,desc,maxlen=30):
     '''prints a matrix, used by sqlpython to print queries' result sets'''