From 4f03593176b220f48e565bdf4df13e439f821e64 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 19 Mar 2021 04:16:06 +0900 Subject: [PATCH 1/3] Avoid extra `BindParam` allocation to generate placeholder in queries Resolves https://github.com/rsim/oracle-enhanced/issues/2152 and follow https://github.com/rails/rails/pull/41577. The tests that fail below have not yet been resolved. https://github.com/rails/rails/commit/6ee96a8f42d6b13bffd46342248f447d9f289288. --- lib/arel/visitors/oracle.rb | 4 ++++ lib/arel/visitors/oracle12.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb index ceb83558d..aa14f29d1 100644 --- a/lib/arel/visitors/oracle.rb +++ b/lib/arel/visitors/oracle.rb @@ -192,6 +192,10 @@ def split_order_string(string) array end + def visit_ActiveModel_Attribute(o, collector) + collector.add_bind(o) { |i| ":a#{i}" } + end + def visit_Arel_Nodes_BindParam(o, collector) collector.add_bind(o.value) { |i| ":a#{i}" } end diff --git a/lib/arel/visitors/oracle12.rb b/lib/arel/visitors/oracle12.rb index 480f88c3b..8a8758d76 100644 --- a/lib/arel/visitors/oracle12.rb +++ b/lib/arel/visitors/oracle12.rb @@ -99,6 +99,10 @@ def visit_Arel_Nodes_UpdateStatement(o, collector) super end + def visit_ActiveModel_Attribute(o, collector) + collector.add_bind(o) { |i| ":a#{i}" } + end + def visit_Arel_Nodes_BindParam(o, collector) collector.add_bind(o.value) { |i| ":a#{i}" } end From 61dae2cb8f454acddf6abe83d8610bbda3a64922 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 30 Mar 2021 22:59:19 +0900 Subject: [PATCH 2/3] Fix build error Follow https://github.com/rsim/oracle-enhanced/pull/2157#issuecomment-810238874 --- .../connection_adapters/oracle_enhanced/database_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb b/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb index 541446338..631dcffab 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb @@ -79,7 +79,7 @@ def explain(arel, binds = []) # New method in ActiveRecord 3.1 # Will add RETURNING clause in case of trigger generated primary keys def sql_for_insert(sql, pk, binds) - unless pk == false || pk.nil? || pk.is_a?(Array) + unless pk == false || pk.nil? || pk.is_a?(Array) || pk.is_a?(String) sql = "#{sql} RETURNING #{quote_column_name(pk)} INTO :returning_id" (binds = binds.dup) << ActiveRecord::Relation::QueryAttribute.new("returning_id", nil, Type::OracleEnhanced::Integer.new) end From d105887e3881672739958b0b08a0d850466c5e29 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 30 Mar 2021 23:07:49 +0900 Subject: [PATCH 3/3] Suppress RuboCop's offense This PR commit suppresses and auto-corrects the following RuboCop's offense. ```console % bundle exec rubocop -a Inspecting 70 files ...................W.................................................. Offenses: lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:13:52: C: [Corrected] Style/RedundantBegin: Redundant begin block detected. self.class.quoted_column_names[name] ||= begin ^^^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:14:11: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation. "\"#{name.upcase}\"" ^^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:15:13: C: [Corrected] Layout/ElseAlignment: Align else with self.class.quoted_column_names[name]. else ^^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:16:15: C: [Corrected] Layout/CommentIndentation: Incorrect indentation detected (column 14 instead of 12). # remove double quotes which cannot be used inside quoted identifier ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:17:11: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation. "\"#{name.gsub('"', '')}\"" ^^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:18:13: W: [Corrected] Layout/EndAlignment: end at 18, 12 is not aligned with self.class.quoted_column_names[name] ||= if at 13, 10. end ^^^ lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:19:1: C: [Corrected] Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body end. lib/active_record/connection_adapters/oracle_enhanced/quoting.rb:19:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected. 70 files inspected, 8 offenses detected, 8 offenses corrected ``` --- .../connection_adapters/oracle_enhanced/quoting.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/active_record/connection_adapters/oracle_enhanced/quoting.rb b/lib/active_record/connection_adapters/oracle_enhanced/quoting.rb index f8be18e94..5c10f6553 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced/quoting.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced/quoting.rb @@ -10,14 +10,11 @@ module Quoting def quote_column_name(name) #:nodoc: name = name.to_s - self.class.quoted_column_names[name] ||= begin - # if only valid lowercase column characters in name - if /\A[a-z][a-z_0-9$#]*\Z/.match?(name) - "\"#{name.upcase}\"" - else - # remove double quotes which cannot be used inside quoted identifier - "\"#{name.gsub('"', '')}\"" - end + self.class.quoted_column_names[name] ||= if /\A[a-z][a-z_0-9$#]*\Z/.match?(name) + "\"#{name.upcase}\"" + else + # remove double quotes which cannot be used inside quoted identifier + "\"#{name.gsub('"', '')}\"" end end