You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2011/06/09 20:32:43 UTC

svn commit: r1134028 - in /cassandra/drivers/py: cql/cursor.py cqlsh

Author: jbellis
Date: Thu Jun  9 18:32:43 2011
New Revision: 1134028

URL: http://svn.apache.org/viewvc?rev=1134028&view=rev
Log:
add print_static_result
patch by jbellis

Modified:
    cassandra/drivers/py/cql/cursor.py
    cassandra/drivers/py/cqlsh

Modified: cassandra/drivers/py/cql/cursor.py
URL: http://svn.apache.org/viewvc/cassandra/drivers/py/cql/cursor.py?rev=1134028&r1=1134027&r2=1134028&view=diff
==============================================================================
--- cassandra/drivers/py/cql/cursor.py (original)
+++ cassandra/drivers/py/cql/cursor.py Thu Jun  9 18:32:43 2011
@@ -31,6 +31,7 @@ from cql.cassandra.ttypes import (
     SchemaDisagreementException)
 
 _COUNT_DESCRIPTION = (None, None, None, None, None, None, None)
+_VOID_DESCRIPTION = (None)
 
 class Cursor:
 
@@ -148,13 +149,19 @@ class Cursor:
             self.rowcount = len(self.result)
             if self.result:
                 self.description = self.decoder.decode_description(self._query_ks, self._query_cf, self.result[0])
-
-        if response.type == CqlResultType.INT:
+        elif response.type == CqlResultType.INT:
             self.result = [(response.num,)]
             self.rs_idx = 0
             self.rowcount = 1
             # TODO: name could be the COUNT expression
             self.description = _COUNT_DESCRIPTION
+        elif response.type == CqlResultType.VOID:
+            self.result = []
+            self.rs_idx = 0
+            self.rowcount = 0
+            self.description = _VOID_DESCRIPTION
+        else:
+            raise Exception('unknown result type ' + response.type)
 
         # 'Return values are not defined.'
         return True
@@ -174,6 +181,9 @@ class Cursor:
 
     def fetchone(self):
         self.__checksock()
+        if self.rs_idx == len(self.result):
+            return None
+
         row = self.result[self.rs_idx]
         self.rs_idx += 1
         if self.description != _COUNT_DESCRIPTION:
@@ -198,18 +208,22 @@ class Cursor:
         return self.fetchmany(len(self.result) - self.rs_idx)
 
     ###
+    # extra, for cqlsh
+    ###
+    
+    def _reset(self):
+        self.rs_idx = 0
+
+    ###
     # Iterator extension
     ###
 
     def next(self):
-        raise Warning("DB-API extension cursor.next() used")
-
         if self.rs_idx >= len(self.result):
             raise StopIteration
         return self.fetchone()
 
     def __iter__(self):
-        raise Warning("DB-API extension cursor.__iter__() used")
         return self
 
     ###

Modified: cassandra/drivers/py/cqlsh
URL: http://svn.apache.org/viewvc/cassandra/drivers/py/cqlsh?rev=1134028&r1=1134027&r2=1134028&view=diff
==============================================================================
--- cassandra/drivers/py/cqlsh (original)
+++ cassandra/drivers/py/cqlsh Thu Jun  9 18:32:43 2011
@@ -16,6 +16,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from collections import defaultdict
 from optparse import OptionParser
 from StringIO import StringIO
 
@@ -24,6 +25,7 @@ import sys
 import readline
 import os
 import re
+import string
 import time
 
 try:
@@ -31,7 +33,7 @@ try:
 except ImportError:
     sys.path.append(os.path.abspath(os.path.dirname(__file__)))
     import cql
-from cql.cursor import _COUNT_DESCRIPTION
+from cql.cursor import _COUNT_DESCRIPTION, _VOID_DESCRIPTION
 
 HISTORY = os.path.join(os.path.expanduser('~'), '.cqlsh')
 CQLTYPES = ("bytes", "ascii", "utf8", "timeuuid", "uuid", "long", "int")
@@ -138,19 +140,76 @@ class Shell(cmd.Cmd):
                 self.printerr("Attempt #%d: %s" % (i, str(err)))
                 time.sleep(1*i)
 
-        if self.cursor.description is _COUNT_DESCRIPTION:
-            if self.cursor.result: print self.cursor.result[0]
+        if self.cursor.description is _VOID_DESCRIPTION:
+            return
+        elif self.cursor.description is _COUNT_DESCRIPTION:
+            self.print_count_result()
         else:
-            for x in range(self.cursor.rowcount):
-                row = self.cursor.fetchone()
-                self.printout(repr(row[0]), BLUE, False)
-                for (i, value) in enumerate(row[1:]):
-                    name = self.cursor.description[i+1][0]
-                    self.printout(" | ", newline=False)
-                    self.printout(repr(name), MAGENTA, False)
-                    self.printout(",", newline=False)
-                    self.printout(repr(value), YELLOW, False)
-                self.printout("")
+            self.print_result()
+
+    def print_count_result(self):
+        if not self.cursor.result:
+            return
+        print 'count'
+        print '-----'
+        print self.cursor.result[0]
+        self.printout("")
+
+    def print_result(self):
+        # first pass: see if we have a static column set
+        last_description = None
+        for row in self.cursor:
+            if last_description is not None and self.cursor.description != last_description:
+                static = False
+                break
+        else:
+            static = True
+        self.cursor._reset()
+
+        if static:
+            self.print_static_result()
+        else:
+            self.print_dynamic_result()
+        self.printout("")
+
+    def print_static_result(self):
+        # first pass, get widths
+        widths = defaultdict(lambda: 0)
+        for row in self.cursor:
+            for desc, value in zip(self.cursor.description, row):
+                name = desc[0]
+                widths[name] = max(widths[name], len(str(name)), len(str(value)))
+        self.cursor._reset()
+                
+        # print header
+        for desc in self.cursor.description:
+            name = desc[0]
+            width = widths[name]
+            self.printout(" ", newline=False)
+            self.printout(string.ljust(str(name), width), MAGENTA, False)
+            self.printout(" |", newline=False)
+        self.printout("")
+
+        # print row data
+        for row in self.cursor:
+            for desc, value in zip(self.cursor.description, row):
+                name = desc[0]
+                width = widths[desc[0]]
+                self.printout(" ", newline=False)
+                self.printout(string.ljust(str(value), width), YELLOW, False)
+                self.printout(" |", newline=False)
+            self.printout("")
+
+    def print_dynamic_result(self):
+        for row in self.cursor:
+            self.printout(" ", newline=False)
+            for desc, value in zip(self.cursor.description, row):
+                name = desc[0]
+                self.printout(str(name), MAGENTA, False)
+                self.printout(",", newline=False)
+                self.printout(str(value), YELLOW, False)
+                self.printout(" | ", newline=False)
+            self.printout("")
 
     def emptyline(self):
         pass
@@ -273,4 +332,3 @@ if __name__ == '__main__':
             print
         except Exception, err:
             shell.printerr("Exception: %s" % err)
-