Class: SqlBeautifier::DeleteQuery

Inherits:
Base
  • Object
show all
Includes:
DmlRendering
Defined in:
lib/sql_beautifier/delete_query.rb

Constant Summary collapse

CLAUSE_KEYWORDS =
%w[using where returning].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(normalized_sql, depth: 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sql_beautifier/delete_query.rb', line 16

def self.parse(normalized_sql, depth: 0)
  scanner = Scanner.new(normalized_sql)
  return nil unless scanner.keyword_at?("delete")

  scanner.skip_past_keyword!("delete")

  return nil unless scanner.keyword_at?("from")

  scanner.skip_past_keyword!("from")
  return nil if scanner.keyword_at?("only")

  table_name = scanner.read_identifier!
  return nil unless table_name

  scanner.skip_whitespace!

  table_alias = parse_table_alias(scanner)

  remaining_text = normalized_sql[scanner.position..].strip
  if remaining_text.present?
    remaining_scanner = Scanner.new(remaining_text)
    return nil unless CLAUSE_KEYWORDS.any? { |keyword| remaining_scanner.keyword_at?(keyword) }
  end

  clauses = split_delete_clauses(normalized_sql, scanner.position)

  new(
    table_name: table_name,
    table_alias: table_alias,
    using_clause: clauses[:using_clause],
    where_clause: clauses[:where_clause],
    returning_clause: clauses[:returning_clause],
    depth: depth
  )
end

Instance Method Details

#render ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/sql_beautifier/delete_query.rb', line 52

def render
  output = +""
  output << render_delete
  output << render_from
  output << render_using if @using_clause
  output << render_where if @where_clause
  output << render_returning if @returning_clause

  "#{output}\n"
end