Skip to content

Commit

Permalink
Merge pull request #2157 from koic/avoid_extra_bind_allocation
Browse files Browse the repository at this point in the history
Avoid extra `BindParam` allocation to generate placeholder in queries
  • Loading branch information
yahonda committed Mar 30, 2021
2 parents 2a2943b + d105887 commit 305e417
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions lib/active_record/connection_adapters/oracle_enhanced/quoting.rb
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/arel/visitors/oracle.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/arel/visitors/oracle12.rb
Expand Up @@ -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
Expand Down

0 comments on commit 305e417

Please sign in to comment.