1
1
module ClosureTree
2
2
module NumericOrderSupport
3
-
4
- def self . adapter_for_connection ( connection )
5
- das = WithAdvisoryLock ::DatabaseAdapterSupport . new ( connection )
6
- if das . postgresql?
7
- ::ClosureTree ::NumericOrderSupport ::PostgreSQLAdapter
8
- elsif das . mysql?
9
- ::ClosureTree ::NumericOrderSupport ::MysqlAdapter
10
- else
11
- ::ClosureTree ::NumericOrderSupport ::GenericAdapter
12
- end
13
- end
14
-
15
3
module MysqlAdapter
16
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
17
- return if parent_id . nil? && dont_order_roots
4
+ module_function def reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
5
+ return if parent_id . nil? && ct . dont_order_roots
6
+
18
7
min_where = if minimum_sort_order_value
19
- "AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
8
+ "AND #{ ct . quoted_order_column } >= #{ minimum_sort_order_value } "
20
9
else
21
10
""
22
11
end
23
- connection . execute 'SET @i = 0'
24
- connection . execute <<-SQL . squish
25
- UPDATE #{ quoted_table_name }
26
- SET #{ quoted_order_column } = (@i := @i + 1) + #{ minimum_sort_order_value . to_i - 1 }
27
- WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where }
28
- ORDER BY #{ nulls_last_order_by }
12
+ ct . connection . execute 'SET @i = 0'
13
+ ct . connection . execute <<-SQL . squish
14
+ UPDATE #{ ct . quoted_table_name }
15
+ SET #{ ct . quoted_order_column } = (@i := @i + 1) + #{ minimum_sort_order_value . to_i - 1 }
16
+ WHERE #{ ct . where_eq ( ct . parent_column_name , parent_id ) } #{ min_where }
17
+ ORDER BY #{ ct . nulls_last_order_by }
29
18
SQL
30
19
end
31
20
end
32
21
33
22
module PostgreSQLAdapter
34
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
35
- return if parent_id . nil? && dont_order_roots
23
+ module_function def reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
24
+ return if parent_id . nil? && ct . dont_order_roots
36
25
min_where = if minimum_sort_order_value
37
- "AND #{ quoted_order_column } >= #{ minimum_sort_order_value } "
26
+ "AND #{ ct . quoted_order_column } >= #{ minimum_sort_order_value } "
38
27
else
39
28
""
40
29
end
41
- connection . execute <<-SQL . squish
42
- UPDATE #{ quoted_table_name }
43
- SET #{ quoted_order_column ( false ) } = t.seq + #{ minimum_sort_order_value . to_i - 1 }
30
+ ct . connection . execute <<-SQL . squish
31
+ UPDATE #{ ct . quoted_table_name }
32
+ SET #{ ct . quoted_order_column ( false ) } = t.seq + #{ minimum_sort_order_value . to_i - 1 }
44
33
FROM (
45
- SELECT #{ quoted_id_column_name } AS id, row_number() OVER(ORDER BY #{ order_by } ) AS seq
46
- FROM #{ quoted_table_name }
47
- WHERE #{ where_eq ( parent_column_name , parent_id ) } #{ min_where }
34
+ SELECT #{ ct . quoted_id_column_name } AS id, row_number() OVER(ORDER BY #{ ct . order_by } ) AS seq
35
+ FROM #{ ct . quoted_table_name }
36
+ WHERE #{ ct . where_eq ( ct . parent_column_name , parent_id ) } #{ min_where }
48
37
) AS t
49
- WHERE #{ quoted_table_name } .#{ quoted_id_column_name } = t.id and
50
- #{ quoted_table_name } .#{ quoted_order_column ( false ) } is distinct from t.seq + #{ minimum_sort_order_value . to_i - 1 }
38
+ WHERE #{ ct . quoted_table_name } .#{ ct . quoted_id_column_name } = t.id and
39
+ #{ ct . quoted_table_name } .#{ ct . quoted_order_column ( false ) } is distinct from t.seq + #{ minimum_sort_order_value . to_i - 1 }
51
40
SQL
52
41
end
53
42
@@ -57,18 +46,31 @@ def rows_updated(result)
57
46
end
58
47
59
48
module GenericAdapter
60
- def reorder_with_parent_id ( parent_id , minimum_sort_order_value = nil )
61
- return if parent_id . nil? && dont_order_roots
62
- scope = model_class .
63
- where ( parent_column_sym => parent_id ) .
64
- order ( nulls_last_order_by )
49
+ module_function def reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
50
+ return if parent_id . nil? && ct . dont_order_roots
51
+ binding . irb
52
+ scope = ct .
53
+ where ( ct . parent_column_sym => parent_id ) .
54
+ order ( ct . nulls_last_order_by )
65
55
if minimum_sort_order_value
66
- scope = scope . where ( "#{ quoted_order_column } >= #{ minimum_sort_order_value } " )
56
+ scope = scope . where ( "#{ ct . quoted_order_column } >= #{ minimum_sort_order_value } " )
67
57
end
68
58
scope . each_with_index do |ea , idx |
69
59
ea . update_order_value ( idx + minimum_sort_order_value . to_i )
70
60
end
71
61
end
72
62
end
63
+
64
+
65
+ module_function def adapter_for_connection ( ct , parent_id , minimum_sort_order_value = nil )
66
+ das = WithAdvisoryLock ::DatabaseAdapterSupport . new ( ct . connection )
67
+ if das . postgresql?
68
+ PostgreSQLAdapter . reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
69
+ elsif das . mysql?
70
+ MysqlAdapter . reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
71
+ else
72
+ GenericAdapter . reorder_with_parent_id ( ct , parent_id , minimum_sort_order_value = nil )
73
+ end
74
+ end
73
75
end
74
76
end
0 commit comments