Skip to content

Commit

Permalink
Merge cb29d59 into cdb775d
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger authored Sep 4, 2017
2 parents cdb775d + cb29d59 commit ba7204c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for crash
Unreleased
==========

- Add support for pasting of multiple statements

- Added back support for multiline table headers

2017/08/21 0.22.1
Expand Down
40 changes: 26 additions & 14 deletions src/crate/crash/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,28 @@ def inner_fn(self, *args):
return inner_fn


def _parse_statements(lines):
"""Return a generator of statements
Args: A list of strings that can contain one or more statements.
Statements are separated using ';' at the end of a line
Everything after the last ';' will be treated as the last statement.
>>> list(_parse_statements(['select * from ', 't1;', 'select name']))
['select * from t1', 'select name']
"""
lines = (l.strip() for l in lines if l)
lines = (l for l in lines if not l.startswith('--'))
parts = []
for line in lines:
parts.append(line.rstrip(';'))
if line.endswith(';'):
yield ' '.join(parts)
parts[:] = []
if parts:
yield ' '.join(parts)


class CrateCmd(object):

def __init__(self,
Expand Down Expand Up @@ -236,22 +258,12 @@ def pprint(self, rows, cols):
self.output_writer.write(result)

def process(self, text):
if text.lstrip().startswith('--'):
return
text = text.rstrip()
if text.startswith('\\') and not self.lines:
self._try_exec_cmd(text.lstrip('\\'))
elif text.endswith(';'):
line = text.rstrip(';')
if self.lines:
self.lines.append(line)
line = ' '.join(self.lines)
self._exec(line)
self.lines[:] = []
else:
self._exec(line)
elif text:
self.lines.append(text)
else:
for statement in _parse_statements(text.split('\n')):
self._exec(statement)
self.lines = []

def exit(self):
if self.lines:
Expand Down
5 changes: 3 additions & 2 deletions src/crate/crash/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def assert_func(self, e, output, err):
self._output_format('csv', assert_func, query)

def test_csv_array_output(self):
query = "select fs['disks']['dev'] from sys.nodes"
query = "select ['/dev/', 'foo'] as arr"

def assert_func(self, e, output, err):
exception_code = e.code
Expand Down Expand Up @@ -289,7 +289,8 @@ def test_multiple_hosts(self):
"-c", "select * from sys.cluster",
"--hosts", self.crate_host, "300.300.300.300:123",
'--history', tmphistory,
'-vv',
'--format', 'tabular',
'-v',
]
with patch('sys.stdout', new_callable=StringIO) as output:
try:
Expand Down
2 changes: 2 additions & 0 deletions src/crate/crash/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import unittest
import doctest
import zc.customdoctests
from . import command
from crate.testing.layer import CrateLayer
from .command import CrateCmd
from .printer import ColorPrinter, PrintWrapper
Expand Down Expand Up @@ -94,6 +95,7 @@ def test_suite():
suite.addTest(s)
CommandTest.layer = crate_layer
CommandTest.crate_host = crate_host
suite.addTest(doctest.DocTestSuite(command))
suite.addTest(unittest.makeSuite(CommandTest))
suite.addTest(unittest.makeSuite(CommandLineArgumentsTest))
suite.addTest(unittest.makeSuite(CommandUtilsTest))
Expand Down

0 comments on commit ba7204c

Please sign in to comment.