Module: SqlBeautifier::StatementSplitter

Defined in:
lib/sql_beautifier/statement_splitter.rb

Constant Summary collapse

STATEMENT_KEYWORDS =
%w[
  select
  with
  create
  insert
  update
  delete
].freeze
BOUNDARY_KEYWORDS =
%w[
  from
  where
  having
  limit
  into
  set
  values
].freeze
CONTINUATION_PAIRS =
{
  "insert" => "select",
}.freeze

Class Method Summary collapse

Class Method Details

.continuation_keyword?(current_keyword, next_keyword) ⇒ Boolean



183
184
185
# File 'lib/sql_beautifier/statement_splitter.rb', line 183

def continuation_keyword?(current_keyword, next_keyword)
  CONTINUATION_PAIRS[current_keyword] == next_keyword
end

.detect_statement_boundaries(sql) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sql_beautifier/statement_splitter.rb', line 102

def detect_statement_boundaries(sql)
  scanner = Scanner.new(sql)
  boundaries = []
  clause_seen = false
  current_statement_keyword = nil
  after_set_operator = false

  until scanner.finished?
    next if scanner.skip_quoted_or_sentinel!

    case scanner.current_char
    when Constants::SINGLE_QUOTE
      scanner.enter_single_quote!
    when Constants::DOUBLE_QUOTE
      scanner.enter_double_quote!
    when Constants::OPEN_PARENTHESIS
      scanner.increment_depth!
      scanner.advance!
    when Constants::CLOSE_PARENTHESIS
      scanner.decrement_depth!
      scanner.advance!
    else
      if scanner.parenthesis_depth.zero?
        matched_set_operator = keyword_match_at(scanner, Constants::SET_OPERATORS)

        if matched_set_operator
          after_set_operator = true
          scanner.advance!(matched_set_operator.length)
          next
        end

        matched_statement_keyword = keyword_match_at(scanner, STATEMENT_KEYWORDS)

        if matched_statement_keyword
          if after_set_operator
            after_set_operator = false
          elsif clause_seen && !continuation_keyword?(current_statement_keyword, matched_statement_keyword)
            boundaries << scanner.position
            clause_seen = false
            current_statement_keyword = matched_statement_keyword
          elsif boundaries.empty?
            boundaries << scanner.position
            current_statement_keyword = matched_statement_keyword
          end

          scanner.advance!(matched_statement_keyword.length)
          next
        end

        matched_boundary_keyword = keyword_match_at(scanner, BOUNDARY_KEYWORDS)

        if matched_boundary_keyword
          clause_seen = true
          scanner.advance!(matched_boundary_keyword.length)
          next
        end
      end

      scanner.advance!
    end
  end

  boundaries
end

.keyword_match_at(scanner, keywords) ⇒ Object



187
188
189
# File 'lib/sql_beautifier/statement_splitter.rb', line 187

def keyword_match_at(scanner, keywords)
  keywords.detect { |keyword| scanner.keyword_at?(keyword) }
end

.merge_trailing_sentinel_segments(statements) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sql_beautifier/statement_splitter.rb', line 167

def merge_trailing_sentinel_segments(statements)
  return statements if statements.length <= 1

  statements.each_with_object([]) do |statement, merged|
    if sentinel_only?(statement) && merged.any?
      merged[-1] = "#{merged[-1]} #{statement}"
    else
      merged << statement
    end
  end
end

.sentinel_only?(segment) ⇒ Boolean



179
180
181
# File 'lib/sql_beautifier/statement_splitter.rb', line 179

def sentinel_only?(segment)
  segment.gsub(CommentParser::SENTINEL_PATTERN, "").strip.empty?
end

.split(sql) ⇒ Object



30
31
32
33
34
# File 'lib/sql_beautifier/statement_splitter.rb', line 30

def split(sql)
  semicolon_chunks = split_on_semicolons(sql)
  statements = semicolon_chunks.flat_map { |chunk| split_concatenated_statements(chunk) }.reject(&:empty?)
  merge_trailing_sentinel_segments(statements)
end

.split_concatenated_statements(sql) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sql_beautifier/statement_splitter.rb', line 80

def split_concatenated_statements(sql)
  boundaries = detect_statement_boundaries(sql)
  return [sql.strip] if boundaries.length <= 1

  statements = []

  boundaries.each_with_index do |boundary_position, index|
    end_position = begin
      if index + 1 < boundaries.length
        boundaries[index + 1]
      else
        sql.length
      end
    end

    statement = sql[boundary_position...end_position].strip
    statements << statement unless statement.empty?
  end

  statements
end

.split_on_semicolons(sql) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sql_beautifier/statement_splitter.rb', line 36

def split_on_semicolons(sql)
  scanner = Scanner.new(sql)
  segments = []
  current_segment = +""

  until scanner.finished?
    consumed = scanner.scan_quoted_or_sentinel!
    if consumed
      current_segment << consumed
      next
    end

    character = scanner.current_char

    case character
    when Constants::OPEN_PARENTHESIS
      scanner.increment_depth!
      current_segment << character

    when Constants::CLOSE_PARENTHESIS
      scanner.decrement_depth!
      current_segment << character

    when ";"
      if scanner.parenthesis_depth.zero?
        stripped_segment = current_segment.strip
        segments << stripped_segment unless stripped_segment.empty?
        current_segment = +""
      else
        current_segment << character
      end

    else
      current_segment << character
    end

    scanner.advance!
  end

  stripped_segment = current_segment.strip
  segments << stripped_segment unless stripped_segment.empty?
  segments
end