diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2013-07-09 10:10:33 +0200 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2013-07-09 10:10:33 +0200 |
| commit | 204b77954eacc85615eace5ac895fab5ebb16e04 (patch) | |
| tree | 111f910d540f97253c77caf05d5b703b8d3cf86d /sqlparse | |
| parent | 223e41045f2b4396007249c59b912aaea149e873 (diff) | |
| download | sqlparse-204b77954eacc85615eace5ac895fab5ebb16e04.tar.gz | |
Add option to truncate long string literals.
Diffstat (limited to 'sqlparse')
| -rw-r--r-- | sqlparse/filters.py | 21 | ||||
| -rw-r--r-- | sqlparse/formatter.py | 17 |
2 files changed, 38 insertions, 0 deletions
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() |
