Class: SqlBeautifier::CreateTableAs
Constant Summary
collapse
- WITH_DATA_SUFFIX_REGEX =
%r{\s+(with\s+(?:no\s+)?data)\s*\z}i
Class Method Summary
collapse
Instance Method Summary
collapse
detect_if_not_exists?, detect_modifier, extended, skip_past_if_not_exists!
Class Method Details
.extract_body(sql, position) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/sql_beautifier/create_table_as.rb', line 45
def self.extract_body(sql, position)
scanner = Scanner.new(sql, position: position)
scanner.skip_whitespace!
return nil if scanner.finished?
if scanner.current_char == Constants::OPEN_PARENTHESIS
closing = scanner.find_matching_parenthesis(scanner.position)
return nil unless closing
body = sql[(scanner.position + 1)...closing].strip
suffix = sql[(closing + 1)..].strip.presence
[body, suffix]
else
extract_unparenthesized_body(sql[scanner.position..].strip)
end
end
|
.extract_unparenthesized_body(raw_body) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/sql_beautifier/create_table_as.rb', line 62
def self.extract_unparenthesized_body(raw_body)
return nil unless raw_body.present?
match = raw_body.match(WITH_DATA_SUFFIX_REGEX)
if match
body = raw_body[0...match.begin(0)].strip
return nil unless body.present?
[body, match[1]]
else
[raw_body, nil]
end
end
|
.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
|
# File 'lib/sql_beautifier/create_table_as.rb', line 16
def self.parse(normalized_sql, depth: 0)
scanner = Scanner.new(normalized_sql)
return nil unless scanner.keyword_at?("create")
scanner.skip_past_keyword!("create")
modifier = detect_modifier(scanner)
scanner.skip_past_keyword!(modifier) if modifier
return nil unless scanner.keyword_at?("table")
scanner.skip_past_keyword!("table")
if_not_exists = detect_if_not_exists?(scanner)
skip_past_if_not_exists!(scanner) if if_not_exists
table_name = scanner.read_identifier!
return nil unless table_name
scanner.skip_whitespace!
return nil unless scanner.keyword_at?("as")
scanner.skip_past_keyword!("as")
body_sql, suffix = extract_body(normalized_sql, scanner.position)
return nil unless body_sql
new(modifier: modifier, if_not_exists: if_not_exists, table_name: table_name, body_sql: body_sql, suffix: suffix, depth: depth)
end
|
Instance Method Details
#render ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/sql_beautifier/create_table_as.rb', line 77
def render
indent_spaces = SqlBeautifier.config_for(:indent_spaces) || 4
formatted = Formatter.new(@body_sql, depth: 0).call
return "#{preamble}\n" unless formatted
indentation = Util.whitespace(indent_spaces)
indented_lines = formatted.chomp.lines.map do |line|
line.strip.empty? ? "\n" : "#{indentation}#{line}"
end.join
formatted_suffix = @suffix ? " #{format_suffix}" : ""
"#{preamble} (\n#{indented_lines}\n)#{formatted_suffix}\n"
end
|