changeset 344:c2e3223703f2

finally desc works in postgres
author Catherine Devlin <catherine.devlin@gmail.com>
date Wed, 22 Apr 2009 10:36:39 -0400
parents d185c87766bd
children af0e16a5e003
files sqlpython/output_templates.py sqlpython/sqlpyPlus.py sqlpython/sqlpython.py
diffstat 3 files changed, 22 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/sqlpython/output_templates.py	Tue Apr 14 16:44:26 2009 -0400
+++ b/sqlpython/output_templates.py	Wed Apr 22 10:36:39 2009 -0400
@@ -57,7 +57,7 @@
 '\\G': genshi.template.NewTextTemplate("""
 {% for (rowNum, row) in enumerate(rows) %}
 **** Row: ${rowNum + 1}
-{% for (colname, itm) in zip(colnames, row) %}${colname.ljust(colnamelen)}: $itm
+{% for (colname, itm) in zip(colnames, row) %}${colname.ljust(max(colnamelengths))}: $itm
 {% end %}{% end %}"""),
 
 '\\i': genshi.template.NewTextTemplate("""{% for (rowNum, row) in enumerate(rows) %}
--- a/sqlpython/sqlpyPlus.py	Tue Apr 14 16:44:26 2009 -0400
+++ b/sqlpython/sqlpyPlus.py	Wed Apr 22 10:36:39 2009 -0400
@@ -629,17 +629,7 @@
                 return
             total_len -= len(rset)
             self.pystate['r'][i] = []
-    
-    def set_query_metadata(self):
-        try:
-            self.tblname = self.tableNameFinder.search(self.querytext).group(1)
-        except AttributeError:
-            self.tblname = ''
-        self.colnames = [d[0] for d in self.curs.description]
-        if outformat in output_templates:
-            self.colnamelen = max(len(colname) for colname in self.colnames)
-            # self.coltypes = [d[1] for d in self.curs.description]   never used?
-                
+            
     def do_select(self, arg, bindVarsIn=None, terminator=None):
         """Fetch rows from a table.
 
@@ -669,10 +659,16 @@
         else: # this is an ugly workaround for the evil paramstyle curse upon DB-API2
             self.curs.execute(self.querytext)
         self.rows = self.curs.fetchmany(min(self.maxfetch, (rowlimit or self.maxfetch)))
+        self.colnames = [d[0] for d in self.curs.description]
+        self.coltypes = [d[1] for d in self.curs.description]
+        self.select_output(selecttext=selecttext, terminator=arg.parsed.terminator, rowlimit=rowlimit)
+        
+    def select_output(self, selecttext, rowlimit=0, terminator=';'):
         self.rc = len(self.rows)
+        self.colnamelengths = [len(d) for d in self.colnames]        
         if self.rc != 0:
             resultset = ResultSet()
-            resultset.colnames = [d[0].lower() for d in self.curs.description]
+            resultset.colnames = [d.lower() for d in self.colnames]
             resultset.pystate = self.pystate
             resultset.statement = 'select ' + selecttext
             resultset.varsUsed = self.varsUsed
@@ -681,8 +677,7 @@
                 row.resultset = resultset
             self.pystate['r'].append(resultset)
             self.age_out_resultsets()
-            self.set_query_metadata()
-            self.stdout.write('\n%s\n' % (self.output(arg.parsed.terminator, rowlimit)))
+            self.stdout.write('\n%s\n' % (self.output(terminator, rowlimit)))
         if self.rc == 0:
             self.pfeedback('\nNo rows Selected.\n')
         elif self.rc == 1: 
@@ -944,7 +939,7 @@
         else:
             result = datatype
         return result     
-                            
+
     @options([make_option('-l', '--long', action='store_true', help='include column #, comments')])
     def do_describe(self, arg, opts):
         schema = self.connections[self.connection_number]['gerald']().schema
@@ -956,10 +951,13 @@
                     self.tblname = objname
                     columns = obj.columns.values()
                     columns.sort()
-                    self.rows = [c[0], c[1], self._str_datatype_(c[2], c[3], c[4], c[5]), c[6], c[7] for c in columns]
+                    self.rows = [(c[0], c[1], self._str_datatype_(c[2], c[3], c[4], c[5]), c[6], c[7]) 
+                                 for c in columns]
                     self.colnames = 'position name type nullable default'.split()
-                    self.colnamelen = max(len(colname) for colname in self.colnames)
-                    self.output(arg.parsed.terminator, rowlimit)
+                    self.coltypes = [str, str, str, str, str]
+                    self.varsUsed = {}
+                    self.select_output(selecttext='describe ' + target, 
+                                       terminator=arg.parsed.terminator, rowlimit=arg.parsed.suffix)
         
     def do_deps(self, arg):
         '''Lists all objects that are dependent upon the object.'''
--- a/sqlpython/sqlpython.py	Tue Apr 14 16:44:26 2009 -0400
+++ b/sqlpython/sqlpython.py	Wed Apr 22 10:36:39 2009 -0400
@@ -333,21 +333,17 @@
         return val
     def pmatrix(self,rows,desc,maxlen=30,heading=True,restructuredtext=False):
         '''prints a matrix, used by sqlpython to print queries' result sets'''
-        names = []
-        maxen = []
+        names = self.colnames[:]
+        maxen = self.colnamelengths[:]
         toprint = []
-        for d in desc:
-            n = d[0]
-            names.append(n)      # list col names
-            maxen.append(len(n)) # col length
-        rcols = range(len(desc))
+        rcols = range(len(self.colnames))
         rrows = range(len(rows))
         for i in rrows:          # loops for all rows
             rowsi = map(str, rows[i]) # current row to process
             split = []                # service var is row split is needed
             mustsplit = 0             # flag 
             for j in rcols:
-                if str(desc[j][1]) == "<type 'cx_Oracle.BINARY'>":  # handles RAW columns
+                if str(self.coltypes[j]) == "<type 'cx_Oracle.BINARY'>":  # handles RAW columns
                     rowsi[j] = binascii.b2a_hex(rowsi[j])
                 maxen[j] = max(maxen[j], len(rowsi[j]))    # computes max field length
                 if maxen[j] <= maxlen:
@@ -369,7 +365,7 @@
             rrows2 = range(len(toprint))
             for j in rrows2:
                 val = toprint[j][i]
-                if str(desc[i][1]) == "<type 'cx_Oracle.NUMBER'>":  # right align numbers
+                if str(self.coltypes[i]) == "<type 'cx_Oracle.NUMBER'>":  # right align numbers
                     toprint[j][i] = (" " * (maxcol-len(val))) + val
                 else:
                     toprint[j][i] = val + (" " * (maxcol-len(val)))