From 79c11f5dcfa65654b5ae5ce89164fbf587943bf6 Mon Sep 17 00:00:00 2001 From: apn <31109952+apnewberry@users.noreply.github.com> Date: Mon, 3 Jun 2019 18:19:50 +0000 Subject: [PATCH 1/2] Separate statements by ``separate_statements`` (int) newlines. --- pglast/printer.py | 2 +- tests/test_printer.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pglast/printer.py b/pglast/printer.py index 6c0232fb..456d81ea 100644 --- a/pglast/printer.py +++ b/pglast/printer.py @@ -227,7 +227,7 @@ def __call__(self, sql, plpgsql=False): else: self.write(';') self.newline() - if self.separate_statements: + for _ in range(int(self.separate_statements)): self.newline() self.print_node(statement) return self.getvalue() diff --git a/tests/test_printer.py b/tests/test_printer.py index b1afbe9c..706df89d 100644 --- a/tests/test_printer.py +++ b/tests/test_printer.py @@ -271,6 +271,24 @@ def raw_stmt(node, output): printer.NODE_PRINTERS.pop('RawStmt', None) +def test_separate_statements(): + """Separate statements by ``separate_statements`` (int) newlines.""" + raw_stmt_printer = printer.NODE_PRINTERS.pop('RawStmt', None) + try: + @printer.node_printer('RawStmt') + def raw_stmt(node, output): + output.write('Yeah') + + output = printer.IndentedStream(separate_statements=2) + result = output('SELECT 1; SELECT 2') + assert result == 'Yeah;\n\n\nYeah' + finally: + if raw_stmt_printer is not None: + printer.NODE_PRINTERS['RawStmt'] = raw_stmt_printer + else: + printer.NODE_PRINTERS.pop('RawStmt', None) + + def test_special_function(): output = printer.RawStream(special_functions=True) From 11b0f00f1730edc36c28edcebac19234775b6661 Mon Sep 17 00:00:00 2001 From: apn <31109952+apnewberry@users.noreply.github.com> Date: Mon, 3 Jun 2019 19:08:45 +0000 Subject: [PATCH 2/2] Change RawStream.separate_statements to int. --- pglast/printer.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pglast/printer.py b/pglast/printer.py index 456d81ea..db774cbb 100644 --- a/pglast/printer.py +++ b/pglast/printer.py @@ -180,9 +180,8 @@ class RawStream(OutputStream): :param int expression_level: start the stream with the given expression level depth, 0 by default - :param bool separate_statements: - ``True`` by default, tells whether multiple statements shall be separated by an - empty line + :param int separate_statements: + ``1`` by default, tells how many empty lines should separate statements :param bool special_functions: ``False`` by default, when ``True`` some functions are treated in a special way and emitted as equivalent constructs @@ -195,7 +194,7 @@ class RawStream(OutputStream): without any adornment. """ - def __init__(self, expression_level=0, separate_statements=True, special_functions=False, + def __init__(self, expression_level=0, separate_statements=1, special_functions=False, comma_at_eoln=False): super().__init__() self.expression_level = expression_level