Skip to content

Commit ccb2021

Browse files
committed
Need to include clearing query cache for delete/update
1 parent 030fb6c commit ccb2021

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,38 @@ def internal_exec_sql_query(sql, conn)
6666

6767
# Executes the delete statement and returns the number of rows affected.
6868
def delete(arel, name = nil, binds = [])
69+
# Clear query cache if the connection pool is configured to do so.
70+
if pool.dirties_query_cache
71+
ActiveRecord::Base.clear_query_caches_for_current_thread
72+
end
73+
6974
intent = QueryIntent.new(arel: arel, name: name, binds: binds)
7075

7176
# Compile Arel to get SQL
7277
compile_arel_in_intent(intent)
7378

74-
# Start of monkey-patch
79+
# Add `SELECT @@ROWCOUNT` to the end of the SQL to get the number of affected rows. This is needed because SQL Server does not return the number of affected rows in the same way as other databases.
7580
sql = intent.processed_sql.present? ? intent.processed_sql : intent.raw_sql
7681
intent.processed_sql = "#{sql}; SELECT @@ROWCOUNT AS AffectedRows"
77-
# End of monkey-patch
7882

7983
affected_rows(raw_execute(intent))
8084
end
8185

8286
# Executes the update statement and returns the number of rows affected.
8387
def update(arel, name = nil, binds = [])
88+
# Clear query cache if the connection pool is configured to do so.
89+
if pool.dirties_query_cache
90+
ActiveRecord::Base.clear_query_caches_for_current_thread
91+
end
92+
8493
intent = QueryIntent.new(arel: arel, name: name, binds: binds)
8594

8695
# Compile Arel to get SQL
8796
compile_arel_in_intent(intent)
8897

89-
# Start of monkey-patch
98+
# Add `SELECT @@ROWCOUNT` to the end of the SQL to get the number of affected rows. This is needed because SQL Server does not return the number of affected rows in the same way as other databases.
9099
sql = intent.processed_sql.present? ? intent.processed_sql : intent.raw_sql
91100
intent.processed_sql = "#{sql}; SELECT @@ROWCOUNT AS AffectedRows"
92-
# End of monkey-patch
93101

94102
affected_rows(raw_execute(intent))
95103
end

0 commit comments

Comments
 (0)