summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters.py21
-rw-r--r--sqlparse/formatter.py17
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()