-
Notifications
You must be signed in to change notification settings - Fork 0
Add DROP TABLE and CREATE TABLE (DDL) formatting #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SqlBeautifier | ||
| class CreateTable < Base | ||
| extend CreateTableParsing | ||
|
|
||
| option :modifier, default: -> {} | ||
| option :if_not_exists, type: Types::Bool | ||
| option :table_name | ||
| option :column_definitions | ||
|
|
||
| def self.parse(normalized_sql, **) | ||
| 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 if scanner.finished? | ||
| return nil unless scanner.current_char == Constants::OPEN_PARENTHESIS | ||
|
|
||
| column_definitions = extract_column_definitions(normalized_sql, scanner) | ||
| return nil unless column_definitions | ||
|
|
||
| new(modifier: modifier, if_not_exists: if_not_exists, table_name: table_name, column_definitions: column_definitions) | ||
|
kinnell marked this conversation as resolved.
|
||
| end | ||
|
|
||
| def self.extract_column_definitions(normalized_sql, scanner) | ||
| closing = scanner.find_matching_parenthesis(scanner.position) | ||
| return nil unless closing | ||
|
|
||
| inner_text = normalized_sql[(scanner.position + 1)...closing].strip | ||
| return nil if inner_text.empty? | ||
|
|
||
| trailing_text = normalized_sql[(closing + 1)..].strip | ||
| return nil unless trailing_text.empty? | ||
|
|
||
| inner_text | ||
| end | ||
|
|
||
| private_class_method :extract_column_definitions | ||
|
|
||
| def render | ||
| parts = [Util.format_keyword("create")] | ||
| parts << Util.format_keyword(@modifier) if @modifier | ||
| parts << Util.format_keyword("table") | ||
| parts << "#{Util.format_keyword('if')} #{Util.format_keyword('not')} #{Util.format_keyword('exists')}" if @if_not_exists | ||
| parts << Util.format_table_name(@table_name) | ||
|
|
||
| "#{parts.join(' ')} (#{@column_definitions})\n" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SqlBeautifier | ||
| module CreateTableParsing | ||
| def self.extended(base) | ||
| base.private_class_method :detect_modifier | ||
| base.private_class_method :detect_if_not_exists? | ||
| base.private_class_method :skip_past_if_not_exists! | ||
| end | ||
|
|
||
| def detect_modifier(scanner) | ||
| Constants::TABLE_MODIFIERS.detect { |modifier| scanner.keyword_at?(modifier) } | ||
| end | ||
|
|
||
| def detect_if_not_exists?(scanner) | ||
| return false unless scanner.keyword_at?("if") | ||
|
|
||
| probe = Scanner.new(scanner.source, position: scanner.position) | ||
| probe.skip_past_keyword!("if") | ||
| return false unless probe.keyword_at?("not") | ||
|
|
||
| probe.skip_past_keyword!("not") | ||
| probe.keyword_at?("exists") | ||
| end | ||
|
|
||
| def skip_past_if_not_exists!(scanner) | ||
| scanner.skip_past_keyword!("if") | ||
| scanner.skip_past_keyword!("not") | ||
| scanner.skip_past_keyword!("exists") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SqlBeautifier | ||
| class DropTable < Base | ||
| option :table_name | ||
| option :if_exists, type: Types::Bool | ||
|
|
||
| def self.parse(normalized_sql, **) | ||
| scanner = Scanner.new(normalized_sql) | ||
| return nil unless scanner.keyword_at?("drop") | ||
|
|
||
| scanner.skip_past_keyword!("drop") | ||
| return nil unless scanner.keyword_at?("table") | ||
|
|
||
| scanner.skip_past_keyword!("table") | ||
|
|
||
| if_exists = false | ||
|
|
||
| if scanner.keyword_at?("if") | ||
| return nil unless detect_if_exists?(scanner) | ||
|
|
||
| skip_past_if_exists!(scanner) | ||
| if_exists = true | ||
| end | ||
|
|
||
| table_name = scanner.read_identifier! | ||
| return nil unless table_name | ||
|
|
||
|
kinnell marked this conversation as resolved.
|
||
| scanner.skip_whitespace! | ||
| return nil unless scanner.finished? | ||
|
|
||
| new(table_name: table_name, if_exists: if_exists) | ||
| end | ||
|
|
||
| def self.detect_if_exists?(scanner) | ||
| probe = Scanner.new(scanner.source, position: scanner.position) | ||
| probe.skip_past_keyword!("if") | ||
| probe.keyword_at?("exists") | ||
| end | ||
|
|
||
| def self.skip_past_if_exists!(scanner) | ||
| scanner.skip_past_keyword!("if") | ||
| scanner.skip_past_keyword!("exists") | ||
| end | ||
|
|
||
| private_class_method :detect_if_exists?, :skip_past_if_exists! | ||
|
|
||
| def render | ||
| parts = [Util.format_keyword("drop"), Util.format_keyword("table")] | ||
| parts << "#{Util.format_keyword('if')} #{Util.format_keyword('exists')}" if @if_exists | ||
| parts << Util.format_table_name(@table_name) | ||
|
|
||
| "#{parts.join(' ')}\n" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.