Class: Niceql::Prettifier::QueryNormalizer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/niceql.rb

Overview

The normalizing and formatting logic:

  1. Split the original query onto the query part + literals + comments

a. find all potential dollar-signed separators
b. prepare full literal extractor regex
  1. Find and separate all literals and comments into mutable/format-able types and immutable ( see the typing and formatting rules below )

  2. Replace all literals and comments with uniq ids on the original query to get the parametrized query

  3. Format parametrized query alongside with mutable/format-able comments and literals

a. clear space characters: replace all \s+ to \s, remove all "\n" e.t.c
b. split in lines -> indent -> colorize
  1. Restore literals and comments with their values

Constant Summary collapse

COMMENT_AND_LITERAL_TYPES =

newline_end_comments: SELECT * – get all column SELECT * /* get all column */

SELECT * – get all column – we need all columns for this request SELECT * /* get all column we need all columns for this request */

rare case newline_start_comments: SELECT * /* get all column we need all columns for this request */ FROM table

newline_wrapped_comments: SELECT * /* get all column we need all columns for this request */ FROM table

SELECT * – get all column – we need all columns for this request FROM … Potentially we could prettify different type of comments and strings a little bit differently, but right now there is no difference between the newline_wrapped_comment, newline_start_comment, newline_end_comment, they all will be wrapped in newlines

[:immutable_string, :indentable_string, :inline_comment, :newline_wrapped_comment,
:newline_start_comment, :newline_end_comment,]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql, colorize) ⇒ QueryNormalizer

Returns a new instance of QueryNormalizer.



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/niceql.rb', line 225

def initialize(sql, colorize)
  @initial_sql = sql
  @colorize = colorize
  @parametrized_sql = ""
  @guids_to_content = {}
  @literals_and_comments_types = {}
  @counter = Hash.new(0)

  init_strings_regex
  prepare_parametrized_sql
  prettify_parametrized_sql
end

Instance Attribute Details

#colorizeObject (readonly)

Returns the value of attribute colorize.



223
224
225
# File 'lib/niceql.rb', line 223

def colorize
  @colorize
end

#initial_sqlObject (readonly)

Returns the value of attribute initial_sql.



223
224
225
# File 'lib/niceql.rb', line 223

def initial_sql
  @initial_sql
end

#literals_and_comments_typesObject (readonly)

Returns the value of attribute literals_and_comments_types.



223
224
225
# File 'lib/niceql.rb', line 223

def literals_and_comments_types
  @literals_and_comments_types
end

#parametrized_sqlObject (readonly)

Returns the value of attribute parametrized_sql.



223
224
225
# File 'lib/niceql.rb', line 223

def parametrized_sql
  @parametrized_sql
end

#string_regexObject (readonly)

Returns the value of attribute string_regex.



223
224
225
# File 'lib/niceql.rb', line 223

def string_regex
  @string_regex
end

Instance Method Details

#prettified_sqlObject



238
239
240
# File 'lib/niceql.rb', line 238

def prettified_sql
  @parametrized_sql % @guids_to_content.transform_keys(&:to_sym)
end