Skip to content

Commit bc75817

Browse files
author
Aidan Haran
committed
Use the query lock hint when joining tables
1 parent 1f870c5 commit bc75817

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## v5.1.5
22

3+
#### Added
4+
5+
* Use lock hint when joining table in query.
6+
37
#### Fixed
48

59
* Memoize `@@version` queries. Fixes #632

lib/arel/visitors/sqlserver.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,29 @@ def visit_Arel_Nodes_JoinSource o, collector
9595
collector = visit_Arel_Nodes_SelectStatement_SQLServer_Lock collector
9696
end
9797
if o.right.any?
98-
collector << " " if o.left
98+
collector << SPACE if o.left
9999
collector = inject_join o.right, collector, ' '
100100
end
101101
collector
102102
end
103103

104+
def visit_Arel_Nodes_InnerJoin o, collector
105+
collector << "INNER JOIN "
106+
collector = visit o.left, collector
107+
collector = visit_Arel_Nodes_SelectStatement_SQLServer_Lock collector, space: true
108+
if o.right
109+
collector << SPACE
110+
visit(o.right, collector)
111+
else
112+
collector
113+
end
114+
end
115+
104116
def visit_Arel_Nodes_OuterJoin o, collector
105117
collector << "LEFT OUTER JOIN "
106118
collector = visit o.left, collector
107119
collector = visit_Arel_Nodes_SelectStatement_SQLServer_Lock collector, space: true
108-
collector << " "
120+
collector << SPACE
109121
visit o.right, collector
110122
end
111123

test/cases/pessimistic_locking_test_sqlserver.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ class PessimisticLockingTestSQLServer < ActiveRecord::TestCase
5252
end
5353
end
5454

55+
describe 'joining tables' do
56+
57+
it 'joined tables use updlock by default' do
58+
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(UPDLOCK\) INNER JOIN \[readers\] WITH\(UPDLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
59+
Person.lock(true).joins(:readers).load
60+
end
61+
end
62+
63+
it 'joined tables can use custom lock directive' do
64+
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(NOLOCK\) INNER JOIN \[readers\] WITH\(NOLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
65+
Person.lock('WITH(NOLOCK)').joins(:readers).load
66+
end
67+
end
68+
69+
it 'left joined tables use updlock by default' do
70+
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(UPDLOCK\) LEFT OUTER JOIN \[readers\] WITH\(UPDLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
71+
Person.lock(true).left_joins(:readers).load
72+
end
73+
end
74+
75+
it 'left joined tables can use custom lock directive' do
76+
assert_sql %r|SELECT \[people\]\.\* FROM \[people\] WITH\(NOLOCK\) LEFT OUTER JOIN \[readers\] WITH\(NOLOCK\)\s+ON \[readers\]\.\[person_id\] = \[people\]\.\[id\]| do
77+
Person.lock('WITH(NOLOCK)').left_joins(:readers).load
78+
end
79+
end
80+
81+
end
82+
5583
end
5684

5785
describe 'For paginated finds' do

0 commit comments

Comments
 (0)