Class: Niceql::Prettifier::QueryNormalizer
- Inherits:
-
Object
- Object
- Niceql::Prettifier::QueryNormalizer
- Extended by:
- Forwardable
- Defined in:
- lib/niceql.rb
Overview
The normalizing and formatting logic:
-
Split the original query onto the query part + literals + comments
a. find all potential dollar-signed separators
b. prepare full literal extractor regex
-
Find and separate all literals and comments into mutable/format-able types and immutable ( see the typing and formatting rules below )
-
Replace all literals and comments with uniq ids on the original query to get the parametrized query
-
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
-
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
-
#colorize ⇒ Object
readonly
Returns the value of attribute colorize.
-
#initial_sql ⇒ Object
readonly
Returns the value of attribute initial_sql.
-
#literals_and_comments_types ⇒ Object
readonly
Returns the value of attribute literals_and_comments_types.
-
#parametrized_sql ⇒ Object
readonly
Returns the value of attribute parametrized_sql.
-
#string_regex ⇒ Object
readonly
Returns the value of attribute string_regex.
Instance Method Summary collapse
-
#initialize(sql, colorize) ⇒ QueryNormalizer
constructor
A new instance of QueryNormalizer.
- #prettified_sql ⇒ Object
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
#colorize ⇒ Object (readonly)
Returns the value of attribute colorize.
223 224 225 |
# File 'lib/niceql.rb', line 223 def colorize @colorize end |
#initial_sql ⇒ Object (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_types ⇒ Object (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_sql ⇒ Object (readonly)
Returns the value of attribute parametrized_sql.
223 224 225 |
# File 'lib/niceql.rb', line 223 def parametrized_sql @parametrized_sql end |
#string_regex ⇒ Object (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_sql ⇒ Object
238 239 240 |
# File 'lib/niceql.rb', line 238 def prettified_sql @parametrized_sql % @guids_to_content.transform_keys(&:to_sym) end |