Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,25 @@ def _exec_insert(intent, pk = nil, sequence_name = nil, returning: nil) # :nodoc
if binds.nil? || binds.empty?
cursor = _connection.prepare(sql)
else
unless @statements.key?(sql)
@statements[sql] = _connection.prepare(sql)
if prepared_statements?
@statements[sql] ||= _connection.prepare(sql)
cursor = @statements[sql]
cached = true
else
cursor = _connection.prepare(sql)
end

cursor = @statements[sql]

cursor.bind_params(type_casted_binds)

bind_specs.each do |position, klass|
cursor.bind_returning_param(position, klass)
end

cached = true
end

cursor.exec_update
rescue
(cursor.close rescue nil) if cursor && !cached
raise
end

rows = []
Expand Down Expand Up @@ -306,16 +309,20 @@ def perform_query(raw_connection, intent)
if binds.nil? || binds.empty?
cursor = raw_connection.prepare(sql)
else
unless @statements.key? sql
@statements[sql] = raw_connection.prepare(sql)
if prepared_statements?
@statements[sql] ||= raw_connection.prepare(sql)
cursor = @statements[sql]
cached = true
else
cursor = raw_connection.prepare(sql)
end

cursor = @statements[sql]
cursor.bind_params(type_casted_binds)

cached = true
end
cursor.exec
rescue
(cursor.close rescue nil) if cursor && !cached
raise
end

columns = cursor.get_col_names.map do |col_name|
Expand Down Expand Up @@ -343,7 +350,7 @@ def with_retry
_connection.with_retry do
yield
rescue
@statements.clear
@statements.clear if prepared_statements?
raise
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class ::TestPost < ActiveRecord::Base
end

it "should clear older cursors when statement limit is reached" do
skip "applies only when prepared statements are enabled" unless @conn.prepared_statements?
binds = [ActiveRecord::Relation::QueryAttribute.new("id", 1, ActiveRecord::Type::OracleEnhanced::Integer.new)]
# free statement pool from dictionary selections to ensure next selects will increase statement pool
@statements.clear
Expand All @@ -311,12 +312,21 @@ class ::TestPost < ActiveRecord::Base
end

it "should cache UPDATE statements with bind variables" do
skip "applies only when prepared statements are enabled" unless @conn.prepared_statements?
expect {
binds = [ActiveRecord::Relation::QueryAttribute.new("id", 1, ActiveRecord::Type::OracleEnhanced::Integer.new)]
@conn.exec_query("UPDATE test_posts SET id = :id", "SQL", binds)
}.to change(@statements, :length).by(+1)
end

it "should not cache UPDATE statements with bind variables when prepared_statements is false" do
skip "applies only when prepared statements are disabled" if @conn.prepared_statements?
expect {
binds = [ActiveRecord::Relation::QueryAttribute.new("id", 1, ActiveRecord::Type::OracleEnhanced::Integer.new)]
@conn.exec_query("UPDATE test_posts SET id = :id", "SQL", binds)
}.not_to change(@statements, :length)
end

it "should not cache UPDATE statements without bind variables" do
expect {
@conn.exec_query("UPDATE test_posts SET id = 1", "SQL", [])
Expand Down