Author: jbellis Date: Tue Apr 19 20:02:16 2011 New Revision: 1095183 URL: http://svn.apache.org/viewvc?rev=1095183&view=rev Log: cqlsh fixes patch by thobbs; reviewed by jbellis for CASSANDRA-2507
Modified: cassandra/branches/cassandra-0.8/drivers/py/cql/cursor.py cassandra/branches/cassandra-0.8/drivers/py/cql/decoders.py cassandra/branches/cassandra-0.8/drivers/py/cqlsh Modified: cassandra/branches/cassandra-0.8/drivers/py/cql/cursor.py URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/drivers/py/cql/cursor.py?rev=1095183&r1=1095182&r2=1095183&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/drivers/py/cql/cursor.py (original) +++ cassandra/branches/cassandra-0.8/drivers/py/cql/cursor.py Tue Apr 19 20:02:16 2011 @@ -62,6 +62,7 @@ class Cursor: def prepare(self, query, params): prepared_query = prepare(query, params) + self._schema_update_needed = False # Snag the keyspace or column family and stash it for later use in # decoding columns. These regexes don't match every query, but the @@ -78,9 +79,7 @@ class Cursor: # If this is a CREATE, then refresh the schema for decoding purposes. match = Cursor._ddl_re.match(prepared_query) if match: - if isinstance(self.decoder, SchemaDecoder): - self.decoder.schema = self.__get_schema() - + self._schema_update_needed = True return prepared_query def __get_schema(self): @@ -110,6 +109,9 @@ class Cursor: def execute(self, cql_query, params={}): self.__checksock() + self.rs_idx = 0 + self.rowcount = 0 + self.description = None try: prepared_q = self.prepare(cql_query, params) except KeyError, e: @@ -132,6 +134,9 @@ class Cursor: except TApplicationException, tapp: raise cql.InternalError("Internal application error") + if self._schema_update_needed and isinstance(self.decoder, SchemaDecoder): + self.decoder.schema = self.__get_schema() + if response.type == CqlResultType.ROWS: self.result = ResultSet(response.rows, self._query_ks, Modified: cassandra/branches/cassandra-0.8/drivers/py/cql/decoders.py URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/drivers/py/cql/decoders.py?rev=1095183&r1=1095182&r2=1095183&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/drivers/py/cql/decoders.py (original) +++ cassandra/branches/cassandra-0.8/drivers/py/cql/decoders.py Tue Apr 19 20:02:16 2011 @@ -26,9 +26,8 @@ class SchemaDecoder(object): self.schema = schema def __get_column_family_def(self, keyspace, column_family): - if self.schema.has_key(keyspace): - if self.schema[keyspace].has_key(column_family): - return self.schema[keyspace][column_family] + if keyspace in self.schema and column_family in self.schema[keyspace]: + return self.schema[keyspace][column_family] return None def __comparator_for(self, keyspace, column_family): Modified: cassandra/branches/cassandra-0.8/drivers/py/cqlsh URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/drivers/py/cqlsh?rev=1095183&r1=1095182&r2=1095183&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/drivers/py/cqlsh (original) +++ cassandra/branches/cassandra-0.8/drivers/py/cqlsh Tue Apr 19 20:02:16 2011 @@ -48,12 +48,13 @@ def startswith(words, text): class Shell(cmd.Cmd): default_prompt = "cqlsh> " continue_prompt = " ... " - + def __init__(self, hostname, port, color=False, username=None, password=None): cmd.Cmd.__init__(self) self.conn = cql.connect(hostname, port, user=username, password=password) - + self.cursor = self.conn.cursor() + if os.path.exists(HISTORY): readline.read_history_file(HISTORY) @@ -112,23 +113,22 @@ class Shell(cmd.Cmd): if not input.strip(): return statement = self.get_statement(input) if not statement: return - - cursor = self.conn.cursor() - cursor.execute(statement) - - if isinstance(cursor.result, ResultSet): - for x in range(cursor.rowcount): - row = cursor.fetchone() - self.printout(row[0], BLUE, False) + + self.cursor.execute(statement) + + if isinstance(self.cursor.result, ResultSet): + 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 = cursor.description[i+1][0] + 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("") else: - if cursor.result: print cursor.result[0] + if self.cursor.result: print self.cursor.result[0] def emptyline(self): pass @@ -137,19 +137,19 @@ class Shell(cmd.Cmd): keywords = ('FIRST', 'REVERSED', 'FROM', 'WHERE', 'KEY') return startswith(keywords, text.upper()) complete_SELECT = complete_select - + def complete_update(self, text, line, begidx, endidx): keywords = ('WHERE', 'KEY', 'SET') return startswith(keywords, text.upper()) complete_UPDATE = complete_update - + def complete_create(self, text, line, begidx, endidx): words = line.split() if len(words) < 3: return startswith(['COLUMNFAMILY', 'KEYSPACE'], text.upper()) - + common = ['WITH', 'AND'] - + if words[1].upper() == 'COLUMNFAMILY': types = startswith(CQLTYPES, text) keywords = startswith(('KEY', 'PRIMARY'), text.upper()) @@ -169,12 +169,12 @@ class Shell(cmd.Cmd): "memtable_operations_in_millions", "replicate_on_write"), text) return startswith(common, text.upper()) + types + keywords + props - + if words[1].upper() == 'KEYSPACE': props = ("replication_factor", "strategy_options", "strategy_class") return startswith(common, text.upper()) + startswith(props, text) complete_CREATE = complete_create - + def complete_drop(self, text, line, begidx, endidx): words = line.split() if len(words) < 3: @@ -189,27 +189,27 @@ class Shell(cmd.Cmd): def set_prompt(self, prompt): if sys.stdin.isatty(): self.prompt = prompt - + def do_EOF(self, arg): if sys.stdin.isatty(): print self.do_exit(None) - + def do_exit(self, arg): sys.exit() do_quit = do_exit - + def printout(self, text, color=None, newline=True, out=sys.stdout): if not color or not self.color: out.write(text) else: out.write(color % text) - + if newline: out.write("\n"); - + def printerr(self, text, color=None, newline=True): self.printout(text, color, newline, sys.stderr) - + if __name__ == '__main__': parser = OptionParser(usage = "Usage: %prog [host [port]]") parser.add_option("-C", @@ -219,9 +219,9 @@ if __name__ == '__main__': parser.add_option("-u", "--username", help="Authenticate as user.") parser.add_option("-p", "--password", help="Authenticate using password.") (options, arguments) = parser.parse_args() - + hostname = len(arguments) > 0 and arguments[0] or "localhost" - + if len(arguments) > 1: try: port = int(arguments[1]) @@ -231,8 +231,8 @@ if __name__ == '__main__': sys.exit(1) else: port = 9160 - - + + shell = Shell(hostname, port, color=options.color, @@ -251,4 +251,4 @@ if __name__ == '__main__': print except Exception, err: shell.printerr("Exception: %s" % err, color=RED) - +