-
Notifications
You must be signed in to change notification settings - Fork 100
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
Encapsulate format logic within Statement and helper classes #162
Open
leboshi
wants to merge
18
commits into
PNixx:master
Choose a base branch
from
magellan-ai:refactor/encapsulate-format-logic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8570d2d
Get rid of :do_execute, since it now has an equivalent signature to :…
leboshi 10a902c
Automagically infer `FORMAT` instead of explicitly passing it as an a…
leboshi 8722d05
Extract all format handling logic into FormatManager object.
leboshi b04ca64
Use a transient settings block to avoid copypasta of underlying abstr…
leboshi 236575b
Encapsulate format and SQL within a Statement object.
leboshi be7495b
Shim old usage of :format with execute, with a deprecation warning.
leboshi fd6f72c
Allow users to pass `nil` to `with_response_format` in order to send …
leboshi 6581809
Move FormatManager and ResponseProcessor within the Statement namespa…
leboshi 4ccddc0
Put do_execute back with a deprecation warning.
leboshi 90082f3
Allow adapter to receive :execute with other statements, too.
leboshi 7fb1a7f
Integrate updates into rebase.
leboshi 061a3e3
Fix logic to make sure settings are correctly reset after using `with…
leboshi a176fa2
Don't deprecate passing :format kwarg to :execute
leboshi dade0fa
Get rid of unnecessary stub.
leboshi a65dffb
Pass SQL to ResponseProcessor for logging.
leboshi dc87568
Specify no format in :exec_insert and :exec_insert_all instead of inf…
leboshi 79e1f24
Use `each_line` instead of `split("\n")`
leboshi 40f13dc
Add default values to :exec_insert to conform to base ActiveRecord de…
leboshi 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 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
30 changes: 30 additions & 0 deletions
30
lib/active_record/connection_adapters/clickhouse/statement.rb
This file contains 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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_record/connection_adapters/clickhouse/statement/format_manager' | ||
require 'active_record/connection_adapters/clickhouse/statement/response_processor' | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module Clickhouse | ||
class Statement | ||
|
||
attr_reader :format | ||
attr_writer :response | ||
|
||
def initialize(sql, format:) | ||
@sql = sql | ||
@format = format | ||
end | ||
|
||
def formatted_sql | ||
@formatted_sql ||= FormatManager.new(@sql, format: @format).apply | ||
end | ||
|
||
def processed_response | ||
ResponseProcessor.new(@response, @format, @sql).process | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
lib/active_record/connection_adapters/clickhouse/statement/format_manager.rb
This file contains 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,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module Clickhouse | ||
class Statement | ||
class FormatManager | ||
|
||
def initialize(sql, format:) | ||
@sql = sql.strip | ||
@format = format | ||
end | ||
|
||
def apply | ||
return @sql if skip_format? || @format.blank? | ||
|
||
"#{@sql} FORMAT #{@format}" | ||
end | ||
|
||
def skip_format? | ||
system_command? || schema_command? || format_specified? || delete? | ||
end | ||
|
||
private | ||
|
||
def system_command? | ||
/\Asystem|\Aoptimize/i.match?(@sql) | ||
end | ||
|
||
def schema_command? | ||
/\Acreate|\Aalter|\Adrop|\Arename/i.match?(@sql) | ||
end | ||
|
||
def format_specified? | ||
/format [a-z]+\z/i.match?(@sql) | ||
end | ||
|
||
def delete? | ||
/\Adelete from/i.match?(@sql) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This incorrect logic for request:
We have a method
exec_insert
which clearly indicates that this is an insert and what response format should be used.