cqlsh: Accept and execute CQL statement(s) from command-line parameter

patch by Mikhail Stepura; reviewed by Brandon Williams for CASSANDRA-7172


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/d48c797f
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/d48c797f
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/d48c797f

Branch: refs/heads/cassandra-2.0
Commit: d48c797f77adf62a7f7365808273edfcbe61cf33
Parents: d13f78a
Author: Mikhail Stepura <mish...@apache.org>
Authored: Thu May 8 14:38:54 2014 -0700
Committer: Mikhail Stepura <mish...@apache.org>
Committed: Thu May 8 15:13:08 2014 -0700

----------------------------------------------------------------------
 CHANGES.txt |  1 +
 bin/cqlsh   | 23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/d48c797f/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 2712398..9e6f173 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -7,6 +7,7 @@
  * Fix disabling autocompaction (CASSANDRA-7187)
  * Fix potential NumberFormatException when deserializing IntegerType 
(CASSANDRA-7088)
  * cqlsh can't tab-complete disabling compaction (CASSANDRA-7185)
+ * cqlsh: Accept and execute CQL statement(s) from command-line parameter 
(CASSANDRA-7172)
 
 
 2.0.8

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d48c797f/bin/cqlsh
----------------------------------------------------------------------
diff --git a/bin/cqlsh b/bin/cqlsh
index ba02e83..6575387 100755
--- a/bin/cqlsh
+++ b/bin/cqlsh
@@ -170,6 +170,7 @@ parser.add_option('--debug', action='store_true',
 parser.add_option('--cqlversion', default=DEFAULT_CQLVER,
                   help='Specify a particular CQL version (default: %default).'
                        ' Examples: "3.0.3", "3.1.0"')
+parser.add_option("-e", "--execute", help='Execute the statement and quit.')
 
 CQL_ERRORS = (cql.Error,)
 try:
@@ -463,7 +464,8 @@ class Shell(cmd.Cmd):
                  cqlver=DEFAULT_CQLVER, keyspace=None,
                  tracing_enabled=False, expand_enabled=False,
                  display_time_format=DEFAULT_TIME_FORMAT,
-                 display_float_precision=DEFAULT_FLOAT_PRECISION):
+                 display_float_precision=DEFAULT_FLOAT_PRECISION,
+                 single_statement=None):
         cmd.Cmd.__init__(self, completekey=completekey)
         self.hostname = hostname
         self.port = port
@@ -521,6 +523,7 @@ class Shell(cmd.Cmd):
         self.query_out = sys.stdout
         self.empty_lines = 0
         self.statement_error = False
+        self.single_statement = single_statement
 
     def set_expanded_cql_version(self, ver):
         ver, vertuple = full_cql_version(ver)
@@ -777,7 +780,11 @@ class Shell(cmd.Cmd):
         with self.prepare_loop():
             while not self.stop:
                 try:
-                    line = self.get_input_line(self.prompt)
+                    if self.single_statement:
+                        line = self.single_statement
+                        self.stop = True
+                    else:
+                        line = self.get_input_line(self.prompt)
                     self.statement.write(line)
                     if self.onecmd(self.statement.getvalue()):
                         self.reset_statement()
@@ -1944,6 +1951,7 @@ def read_options(cmdlineargs, environment):
     optvalues.file = None
     optvalues.tty = sys.stdin.isatty()
     optvalues.cqlversion = option_with_default(configs.get, 'cql', 'version', 
DEFAULT_CQLVER)
+    optvalues.execute = None
 
     (options, arguments) = parser.parse_args(cmdlineargs, values=optvalues)
 
@@ -1958,9 +1966,12 @@ def read_options(cmdlineargs, environment):
     if len(arguments) > 1:
         port = arguments[1]
 
-    if options.file is not None:
+    if options.file or options.execute:
         options.tty = False
 
+    if options.execute and not options.execute.endswith(';'):
+        options.execute += ';'
+
     options.transport_factory = load_factory(options.transport_factory)
 
     if optvalues.color in (True, False):
@@ -2045,7 +2056,8 @@ def main(options, hostname, port):
                       cqlver=options.cqlversion,
                       keyspace=options.keyspace,
                       display_time_format=options.time_format,
-                      display_float_precision=options.float_precision)
+                      display_float_precision=options.float_precision,
+                      single_statement=options.execute)
     except KeyboardInterrupt:
         sys.exit('Connection aborted.')
     except CQL_ERRORS, e:
@@ -2057,7 +2069,8 @@ def main(options, hostname, port):
 
     shell.cmdloop()
     save_history()
-    if options.file and shell.statement_error:
+    batch_mode = options.file or options.execute
+    if batch_mode and shell.statement_error:
         sys.exit(2)
 
 if __name__ == '__main__':

Reply via email to