Method: PgQuery#truncate
- Defined in:
- lib/pg_query/truncate.rb
#truncate(max_length) ⇒ Object
Truncates the query string to be below the specified length, first trying to omit less important parts of the query, and only then cutting off the end.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pg_query/truncate.rb', line 8 def truncate(max_length) output = deparse(@tree) # Early exit if we're already below the max length return output if output.size <= max_length truncations = find_possible_truncations # Truncate the deepest possible truncation that is the longest first truncations.sort_by! { |t| [-t.location.size, -t.length] } tree = deep_dup(@tree) truncations.each do |truncation| next if truncation.length < 3 find_tree_location(tree, truncation.location) do |expr, k| expr[k] = { A_TRUNCATED => nil } expr[k] = [expr[k]] if truncation.is_array end output = deparse(tree) return output if output.size <= max_length end # We couldn't do a proper smart truncation, so we need a hard cut-off output[0..max_length - 4] + '...' end |