summaryrefslogtreecommitdiff
path: root/sqlparse/formatter.py
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2013-07-09 10:10:33 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2013-07-09 10:10:33 +0200
commit204b77954eacc85615eace5ac895fab5ebb16e04 (patch)
tree111f910d540f97253c77caf05d5b703b8d3cf86d /sqlparse/formatter.py
parent223e41045f2b4396007249c59b912aaea149e873 (diff)
downloadsqlparse-204b77954eacc85615eace5ac895fab5ebb16e04.tar.gz
Add option to truncate long string literals.
Diffstat (limited to 'sqlparse/formatter.py')
-rw-r--r--sqlparse/formatter.py17
1 files changed, 17 insertions, 0 deletions
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()