Module: ClickHouse::Client::Redactor
- Defined in:
- lib/click_house/client/redactor.rb
Class Method Summary collapse
-
.redact(query_builder, bind_manager = ClickHouse::Client::BindIndexManager.new) ⇒ String
Redacts the SQL query represented by the query builder.
- .redact_condition(condition, bind_manager) ⇒ Object
- .redact_named_function(condition, bind_manager) ⇒ Object
Class Method Details
.redact(query_builder, bind_manager = ClickHouse::Client::BindIndexManager.new) ⇒ String
Redacts the SQL query represented by the query builder.
Example:
query_builder = ClickHouse::QueryBuilder.new('users').where(name: 'John Doe')
redacted_query = ClickHouse::Redactor.redact(query_builder)
# The redacted_query will contain the SQL query with values replaced by placeholders.
output: "SELECT * FROM \"users\" WHERE \"users\".\"name\" = $1"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/click_house/client/redactor.rb', line 17 def self.redact(query_builder, bind_manager = ClickHouse::Client::BindIndexManager.new) cloned_query_builder = query_builder.clone cloned_query_builder.conditions = cloned_query_builder.conditions.map do |condition| redact_condition(condition, bind_manager) end cloned_query_builder.manager.constraints.clear cloned_query_builder.conditions.each do |condition| cloned_query_builder.manager.where(condition) end visitor = Arel::Visitors::ToSql.new(ClickHouse::Client::ArelEngine.new) visitor.accept(cloned_query_builder.manager.ast, Arel::Collectors::SQLString.new).value end |
.redact_condition(condition, bind_manager) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/click_house/client/redactor.rb', line 33 def self.redact_condition(condition, bind_manager) case condition when Arel::Nodes::In condition.left.in(Array.new(condition.right.size) { Arel.sql(bind_manager.next_bind_str) }) when Arel::Nodes::Equality condition.left.eq(Arel.sql(bind_manager.next_bind_str)) when Arel::Nodes::LessThan condition.left.lt(Arel.sql(bind_manager.next_bind_str)) when Arel::Nodes::LessThanOrEqual condition.left.lteq(Arel.sql(bind_manager.next_bind_str)) when Arel::Nodes::GreaterThan condition.left.gt(Arel.sql(bind_manager.next_bind_str)) when Arel::Nodes::GreaterThanOrEqual condition.left.gteq(Arel.sql(bind_manager.next_bind_str)) when Arel::Nodes::NamedFunction redact_named_function(condition, bind_manager) else raise ArgumentError, "Unsupported Arel node type for Redactor: #{condition.class}" end end |
.redact_named_function(condition, bind_manager) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/click_house/client/redactor.rb', line 54 def self.redact_named_function(condition, bind_manager) redacted_condition = Arel::Nodes::NamedFunction.new(condition.name, condition.expressions.dup) case redacted_condition.name when 'startsWith' redacted_condition.expressions[1] = Arel.sql(bind_manager.next_bind_str) else redacted_condition.expressions = redacted_condition.expressions.map { Arel.sql(bind_manager.next_bind_str) } end redacted_condition end |