From 204b77954eacc85615eace5ac895fab5ebb16e04 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Tue, 9 Jul 2013 10:10:33 +0200 Subject: Add option to truncate long string literals. --- sqlparse/filters.py | 21 +++++++++++++++++++++ sqlparse/formatter.py | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'sqlparse') diff --git a/sqlparse/filters.py b/sqlparse/filters.py index 70f1051..337d3bf 100644 --- a/sqlparse/filters.py +++ b/sqlparse/filters.py @@ -47,6 +47,27 @@ class IdentifierCaseFilter(_CaseFilter): yield ttype, value +class TruncateStringFilter: + + def __init__(self, width, char): + self.width = max(width, 1) + self.char = unicode(char) + + def process(self, stack, stream): + for ttype, value in stream: + if ttype is T.Literal.String.Single: + if value[:2] == '\'\'': + inner = value[2:-2] + quote = u'\'\'' + else: + inner = value[1:-1] + quote = u'\'' + if len(inner) > self.width: + value = u''.join((quote, inner[:self.width], self.char, + quote)) + yield ttype, value + + class GetComments: """Get the comments from a stack""" def process(self, stack, stream): diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index 8761c16..811f5af 100644 --- a/sqlparse/formatter.py +++ b/sqlparse/formatter.py @@ -33,6 +33,19 @@ def validate_options(options): raise SQLParseError('Invalid value for strip_whitespace: %r' % strip_ws) + truncate_strings = options.get('truncate_strings', None) + if truncate_strings is not None: + try: + truncate_strings = int(truncate_strings) + except (ValueError, TypeError): + raise SQLParseError('Invalid value for truncate_strings: %r' + % truncate_strings) + if truncate_strings <= 1: + raise SQLParseError('Invalid value for truncate_strings: %r' + % truncate_strings) + options['truncate_strings'] = truncate_strings + options['truncate_char'] = options.get('truncate_char', '[...]') + reindent = options.get('reindent', False) if reindent not in [True, False]: raise SQLParseError('Invalid value for reindent: %r' @@ -84,6 +97,10 @@ def build_filter_stack(stack, options): stack.preprocess.append( filters.IdentifierCaseFilter(options['identifier_case'])) + if options.get('truncate_strings', None) is not None: + stack.preprocess.append(filters.TruncateStringFilter( + width=options['truncate_strings'], char=options['truncate_char'])) + # After grouping if options.get('strip_comments', False): stack.enable_grouping() -- cgit v1.2.1