From 8072d8653a32e7719342df25eecab688ac9e39cc Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 19 Mar 2019 02:12:44 +0900 Subject: Extract `sanitize_as_sql_comment` from SQL visitor into connection Probably that is useful for any other feature as well. --- .../lib/active_record/connection_adapters/abstract/quoting.rb | 4 ++++ activerecord/lib/arel/visitors/ibm_db.rb | 3 ++- activerecord/lib/arel/visitors/informix.rb | 3 ++- activerecord/lib/arel/visitors/mssql.rb | 3 ++- activerecord/lib/arel/visitors/to_sql.rb | 10 +++++----- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 9d24d839c1..2877530917 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -138,6 +138,10 @@ module ActiveRecord "'#{quote_string(value.to_s)}'" end + def sanitize_as_sql_comment(value) # :nodoc: + value.to_s.gsub(%r{ (/ (?: | \g<1>) \*) \+? \s* | \s* (\* (?: | \g<2>) /) }x, "") + end + private def type_casted_binds(binds) if binds.first.is_a?(Array) diff --git a/activerecord/lib/arel/visitors/ibm_db.rb b/activerecord/lib/arel/visitors/ibm_db.rb index 0ffc0725f7..5cf958f5f0 100644 --- a/activerecord/lib/arel/visitors/ibm_db.rb +++ b/activerecord/lib/arel/visitors/ibm_db.rb @@ -10,7 +10,8 @@ module Arel # :nodoc: all end def visit_Arel_Nodes_OptimizerHints(o, collector) - collector << "/* #{sanitize_as_sql_comment(o).join} */" + hints = o.expr.map { |v| sanitize_as_sql_comment(v) }.join + collector << "/* #{hints} */" end def visit_Arel_Nodes_Limit(o, collector) diff --git a/activerecord/lib/arel/visitors/informix.rb b/activerecord/lib/arel/visitors/informix.rb index cd43be8858..1a4ad1c8d8 100644 --- a/activerecord/lib/arel/visitors/informix.rb +++ b/activerecord/lib/arel/visitors/informix.rb @@ -43,7 +43,8 @@ module Arel # :nodoc: all end def visit_Arel_Nodes_OptimizerHints(o, collector) - collector << "/*+ #{sanitize_as_sql_comment(o).join(", ")} */" + hints = o.expr.map { |v| sanitize_as_sql_comment(v) }.join(", ") + collector << "/*+ #{hints} */" end def visit_Arel_Nodes_Offset(o, collector) diff --git a/activerecord/lib/arel/visitors/mssql.rb b/activerecord/lib/arel/visitors/mssql.rb index 85815baca2..05fe37c26f 100644 --- a/activerecord/lib/arel/visitors/mssql.rb +++ b/activerecord/lib/arel/visitors/mssql.rb @@ -82,7 +82,8 @@ module Arel # :nodoc: all end def visit_Arel_Nodes_OptimizerHints(o, collector) - collector << "OPTION (#{sanitize_as_sql_comment(o).join(", ")})" + hints = o.expr.map { |v| sanitize_as_sql_comment(v) }.join(", ") + collector << "OPTION (#{hints})" end def get_offset_limit_clause(o) diff --git a/activerecord/lib/arel/visitors/to_sql.rb b/activerecord/lib/arel/visitors/to_sql.rb index 583f920290..e44dacd0ff 100644 --- a/activerecord/lib/arel/visitors/to_sql.rb +++ b/activerecord/lib/arel/visitors/to_sql.rb @@ -219,7 +219,8 @@ module Arel # :nodoc: all end def visit_Arel_Nodes_OptimizerHints(o, collector) - collector << "/*+ #{sanitize_as_sql_comment(o).join(" ")} */" + hints = o.expr.map { |v| sanitize_as_sql_comment(v) }.join(" ") + collector << "/*+ #{hints} */" end def collect_nodes_for(nodes, collector, spacer, connector = COMMA) @@ -785,10 +786,9 @@ module Arel # :nodoc: all @connection.quote_column_name(name) end - def sanitize_as_sql_comment(o) - o.expr.map { |v| - v.gsub(%r{ (/ (?: | \g<1>) \*) \+? \s* | \s* (\* (?: | \g<2>) /) }x, "") - } + def sanitize_as_sql_comment(value) + return value if Arel::Nodes::SqlLiteral === value + @connection.sanitize_as_sql_comment(value) end def collect_optimizer_hints(o, collector) -- cgit v1.2.3