composite_primary_keys 12.0.10 → 13.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +896 -880
  3. data/README.rdoc +181 -180
  4. data/Rakefile +37 -37
  5. data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
  6. data/lib/composite_primary_keys/arel/to_sql.rb +18 -18
  7. data/lib/composite_primary_keys/associations/association.rb +23 -23
  8. data/lib/composite_primary_keys/associations/association_scope.rb +4 -6
  9. data/lib/composite_primary_keys/associations/collection_association.rb +31 -31
  10. data/lib/composite_primary_keys/associations/foreign_association.rb +15 -15
  11. data/lib/composite_primary_keys/associations/has_many_association.rb +35 -35
  12. data/lib/composite_primary_keys/associations/{join_dependency.rb → join_association.rb} +137 -103
  13. data/lib/composite_primary_keys/associations/through_association.rb +1 -2
  14. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -2
  15. data/lib/composite_primary_keys/attribute_methods/read.rb +1 -1
  16. data/lib/composite_primary_keys/attribute_methods/write.rb +1 -1
  17. data/lib/composite_primary_keys/attribute_methods.rb +14 -2
  18. data/lib/composite_primary_keys/autosave_association.rb +60 -60
  19. data/lib/composite_primary_keys/base.rb +137 -141
  20. data/lib/composite_primary_keys/composite_arrays.rb +86 -86
  21. data/lib/composite_primary_keys/composite_predicates.rb +2 -1
  22. data/lib/composite_primary_keys/composite_relation.rb +29 -29
  23. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  24. data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
  25. data/lib/composite_primary_keys/counter_cache.rb +15 -15
  26. data/lib/composite_primary_keys/fixtures.rb +21 -21
  27. data/lib/composite_primary_keys/nested_attributes.rb +1 -1
  28. data/lib/composite_primary_keys/persistence.rb +82 -82
  29. data/lib/composite_primary_keys/reflection.rb +64 -2
  30. data/lib/composite_primary_keys/relation/batches.rb +15 -7
  31. data/lib/composite_primary_keys/relation/calculations.rb +104 -81
  32. data/lib/composite_primary_keys/relation/finder_methods.rb +7 -7
  33. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +23 -4
  34. data/lib/composite_primary_keys/relation/query_methods.rb +2 -2
  35. data/lib/composite_primary_keys/relation/where_clause.rb +6 -11
  36. data/lib/composite_primary_keys/relation.rb +13 -9
  37. data/lib/composite_primary_keys/sanitization.rb +42 -42
  38. data/lib/composite_primary_keys/table_metadata.rb +11 -0
  39. data/lib/composite_primary_keys/transactions.rb +34 -34
  40. data/lib/composite_primary_keys/validations/uniqueness.rb +31 -31
  41. data/lib/composite_primary_keys/version.rb +8 -8
  42. data/lib/composite_primary_keys.rb +119 -117
  43. data/scripts/console.rb +48 -48
  44. data/scripts/txt2html +76 -76
  45. data/scripts/txt2js +65 -65
  46. data/tasks/databases/mysql.rake +40 -40
  47. data/tasks/databases/oracle.rake +41 -41
  48. data/tasks/databases/postgresql.rake +38 -38
  49. data/tasks/databases/sqlite.rake +25 -25
  50. data/tasks/databases/sqlserver.rake +43 -43
  51. data/tasks/website.rake +18 -18
  52. data/test/README_tests.rdoc +56 -56
  53. data/test/abstract_unit.rb +114 -114
  54. data/test/connections/connection_spec.rb +27 -27
  55. data/test/connections/databases.example.yml +40 -40
  56. data/test/connections/databases.yml +40 -39
  57. data/test/fixtures/article.rb +10 -10
  58. data/test/fixtures/articles.yml +7 -7
  59. data/test/fixtures/capitol.rb +3 -3
  60. data/test/fixtures/capitols.yml +16 -16
  61. data/test/fixtures/comment.rb +5 -5
  62. data/test/fixtures/comments.yml +17 -17
  63. data/test/fixtures/department.rb +16 -16
  64. data/test/fixtures/departments.yml +4 -0
  65. data/test/fixtures/dorm.rb +2 -2
  66. data/test/fixtures/dorms.yml +4 -4
  67. data/test/fixtures/employee.rb +5 -5
  68. data/test/fixtures/employees.yml +6 -1
  69. data/test/fixtures/group.rb +2 -2
  70. data/test/fixtures/groups.yml +6 -6
  71. data/test/fixtures/membership.rb +2 -0
  72. data/test/fixtures/membership_status.rb +2 -2
  73. data/test/fixtures/membership_statuses.yml +16 -16
  74. data/test/fixtures/memberships.yml +10 -10
  75. data/test/fixtures/product.rb +9 -9
  76. data/test/fixtures/product_tariff.rb +5 -5
  77. data/test/fixtures/product_tariffs.yml +14 -14
  78. data/test/fixtures/products.yml +11 -11
  79. data/test/fixtures/reading.rb +4 -4
  80. data/test/fixtures/readings.yml +10 -10
  81. data/test/fixtures/reference_code.rb +7 -7
  82. data/test/fixtures/reference_codes.yml +28 -28
  83. data/test/fixtures/reference_type.rb +12 -12
  84. data/test/fixtures/reference_types.yml +9 -9
  85. data/test/fixtures/restaurant.rb +9 -9
  86. data/test/fixtures/restaurants.yml +14 -14
  87. data/test/fixtures/restaurants_suburb.rb +2 -2
  88. data/test/fixtures/room.rb +11 -11
  89. data/test/fixtures/room_assignment.rb +13 -13
  90. data/test/fixtures/room_assignments.yml +24 -24
  91. data/test/fixtures/room_attribute.rb +2 -2
  92. data/test/fixtures/room_attribute_assignment.rb +4 -4
  93. data/test/fixtures/room_attribute_assignments.yml +4 -4
  94. data/test/fixtures/room_attributes.yml +2 -2
  95. data/test/fixtures/rooms.yml +12 -12
  96. data/test/fixtures/street.rb +2 -2
  97. data/test/fixtures/student.rb +3 -3
  98. data/test/fixtures/students.yml +15 -15
  99. data/test/fixtures/suburb.rb +5 -5
  100. data/test/fixtures/tariff.rb +5 -5
  101. data/test/fixtures/tariffs.yml +14 -14
  102. data/test/fixtures/topic_sources.yml +3 -3
  103. data/test/fixtures/topics.yml +8 -8
  104. data/test/fixtures/users.yml +10 -10
  105. data/test/plugins/pagination.rb +405 -405
  106. data/test/plugins/pagination_helper.rb +135 -135
  107. data/test/test_associations.rb +372 -358
  108. data/test/test_attribute_methods.rb +63 -63
  109. data/test/test_attributes.rb +19 -4
  110. data/test/test_calculations.rb +9 -2
  111. data/test/test_callbacks.rb +99 -99
  112. data/test/test_composite_arrays.rb +38 -38
  113. data/test/test_counter_cache.rb +30 -30
  114. data/test/test_delete.rb +8 -2
  115. data/test/test_dumpable.rb +15 -15
  116. data/test/test_dup.rb +37 -37
  117. data/test/test_equal.rb +26 -26
  118. data/test/test_find.rb +8 -2
  119. data/test/test_habtm.rb +141 -141
  120. data/test/test_miscellaneous.rb +32 -32
  121. data/test/test_optimistic.rb +18 -18
  122. data/test/test_pagination.rb +35 -35
  123. data/test/test_polymorphic.rb +43 -43
  124. data/test/test_predicates.rb +59 -59
  125. data/test/test_preload.rb +102 -102
  126. data/test/test_santiago.rb +23 -23
  127. data/test/test_touch.rb +23 -23
  128. data/test/test_tutorial_example.rb +25 -25
  129. data/test/test_update.rb +6 -0
  130. data/test/test_validations.rb +13 -13
  131. metadata +13 -12
@@ -70,7 +70,7 @@ module ActiveRecord
70
70
  if target_record
71
71
  existing_record = target_record
72
72
  else
73
- association.add_to_target(existing_record, :skip_callbacks)
73
+ association.add_to_target(existing_record, skip_callbacks: true)
74
74
  end
75
75
 
76
76
  assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
@@ -1,82 +1,82 @@
1
- module ActiveRecord
2
- module Persistence
3
- module ClassMethods
4
- def delete(id_or_array)
5
- # CPK
6
- if self.composite?
7
- id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
8
- [id_or_array]
9
- else
10
- Array(id_or_array)
11
- end
12
-
13
- # Delete should return the number of deleted records
14
- id_or_array.map do |id|
15
- # Is the passed in id actually a record?
16
- id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
17
- delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
18
- end.sum
19
- else
20
- delete_by(primary_key => id_or_array)
21
- end
22
- end
23
-
24
- def _update_record(values, constraints) # :nodoc:
25
- # CPK
26
- if self.composite? && constraints[primary_key]
27
- primary_key_values = constraints.delete(primary_key)
28
- primary_key.each_with_index do |key, i|
29
- constraints[key] = primary_key_values[i]
30
- end
31
- end
32
-
33
- constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
34
-
35
- um = arel_table.where(
36
- constraints.reduce(&:and)
37
- ).compile_update(_substitute_values(values), primary_key)
38
-
39
- connection.update(um, "#{self} Update")
40
- end
41
-
42
- def _delete_record(constraints) # :nodoc:
43
- # CPK
44
- if self.composite? && constraints[primary_key]
45
- primary_key_values = constraints.delete(primary_key)
46
- primary_key.each_with_index do |key, i|
47
- constraints[key] = primary_key_values[i]
48
- end
49
- end
50
-
51
- constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
52
-
53
- dm = Arel::DeleteManager.new
54
- dm.from(arel_table)
55
- dm.wheres = constraints
56
-
57
- connection.delete(dm, "#{self} Destroy")
58
- end
59
- end
60
-
61
- def _create_record(attribute_names = self.attribute_names)
62
- attribute_names = attributes_for_create(attribute_names)
63
-
64
- new_id = self.class._insert_record(
65
- attributes_with_values(attribute_names)
66
- )
67
-
68
- # CPK
69
- if self.composite?
70
- self.id = self.id.zip(Array(new_id)).map {|id1, id2| id2.nil? ? id1 : id2}
71
- else
72
- self.id ||= new_id if self.class.primary_key
73
- end
74
-
75
- @new_record = false
76
-
77
- yield(self) if block_given?
78
-
79
- id
80
- end
81
- end
82
- end
1
+ module ActiveRecord
2
+ module Persistence
3
+ module ClassMethods
4
+ def delete(id_or_array)
5
+ # CPK
6
+ if self.composite?
7
+ id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
8
+ [id_or_array]
9
+ else
10
+ Array(id_or_array)
11
+ end
12
+
13
+ # Delete should return the number of deleted records
14
+ id_or_array.map do |id|
15
+ # Is the passed in id actually a record?
16
+ id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
17
+ delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
18
+ end.sum
19
+ else
20
+ delete_by(primary_key => id_or_array)
21
+ end
22
+ end
23
+
24
+ def _update_record(values, constraints) # :nodoc:
25
+ # CPK
26
+ if self.composite? && constraints[primary_key]
27
+ primary_key_values = constraints.delete(primary_key)
28
+ primary_key.each_with_index do |key, i|
29
+ constraints[key] = primary_key_values[i]
30
+ end
31
+ end
32
+
33
+ constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
34
+
35
+ um = arel_table.where(
36
+ constraints.reduce(&:and)
37
+ ).compile_update(_substitute_values(values), primary_key)
38
+
39
+ connection.update(um, "#{self} Update")
40
+ end
41
+
42
+ def _delete_record(constraints) # :nodoc:
43
+ # CPK
44
+ if self.composite? && constraints[primary_key]
45
+ primary_key_values = constraints.delete(primary_key)
46
+ primary_key.each_with_index do |key, i|
47
+ constraints[key] = primary_key_values[i]
48
+ end
49
+ end
50
+
51
+ constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
52
+
53
+ dm = Arel::DeleteManager.new
54
+ dm.from(arel_table)
55
+ dm.wheres = constraints
56
+
57
+ connection.delete(dm, "#{self} Destroy")
58
+ end
59
+ end
60
+
61
+ def _create_record(attribute_names = self.attribute_names)
62
+ attribute_names = attributes_for_create(attribute_names)
63
+
64
+ new_id = self.class._insert_record(
65
+ attributes_with_values(attribute_names)
66
+ )
67
+
68
+ # CPK
69
+ if self.composite?
70
+ self.id = self.id.zip(Array(new_id)).map {|id1, id2| id2.nil? ? id1 : id2}
71
+ else
72
+ self.id ||= new_id if self.class.primary_key
73
+ end
74
+
75
+ @new_record = false
76
+
77
+ yield(self) if block_given?
78
+
79
+ id
80
+ end
81
+ end
82
+ end
@@ -6,8 +6,8 @@ module ActiveRecord
6
6
  scope_chain_items = join_scopes(table, predicate_builder)
7
7
  klass_scope = klass_join_scope(table, predicate_builder)
8
8
 
9
- key = join_keys.key
10
- foreign_key = join_keys.foreign_key
9
+ key = join_primary_key
10
+ foreign_key = join_foreign_key
11
11
 
12
12
  # CPK
13
13
  #klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
@@ -25,5 +25,67 @@ module ActiveRecord
25
25
  scope_chain_items.inject(klass_scope, &:merge!)
26
26
  end
27
27
  end
28
+
29
+ class AssociationReflection < MacroReflection
30
+ def foreign_key
31
+ # CPK
32
+ # @foreign_key ||= -(options[:foreign_key]&.to_s || derive_foreign_key)
33
+ @foreign_key ||= extract_keys(options[:foreign_key]) || derive_foreign_key
34
+ end
35
+
36
+ def association_foreign_key
37
+ # CPK
38
+ # @association_foreign_key ||= -(options[:association_foreign_key]&.to_s || class_name.foreign_key)
39
+ @association_foreign_key ||= extract_keys(options[:association_foreign_key]) || class_name.foreign_key
40
+ end
41
+
42
+ def active_record_primary_key
43
+ # CPK (Rails freezes the string returned in the expression that calculates PK here. But Rails uses the `-` method which is not available on Array for CPK, so we calculate it in one line and freeze it on the next)
44
+ # @active_record_primary_key ||= -(options[:primary_key]&.to_s || primary_key(active_record))
45
+ @active_record_primary_key ||= begin
46
+ pk = options[:primary_key] || primary_key(active_record)
47
+ pk.freeze
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def extract_keys(keys)
54
+ case keys
55
+ when Array
56
+ keys.map { |k| k.to_s }
57
+ when NilClass
58
+ nil
59
+ else
60
+ keys.to_s
61
+ end
62
+ end
63
+ end
64
+
65
+ class BelongsToReflection < AssociationReflection
66
+ def association_primary_key(klass = nil)
67
+ if primary_key = options[:primary_key]
68
+ # CPK
69
+ # @association_primary_key ||= -primary_key.to_s
70
+ @association_primary_key ||= primary_key.freeze
71
+ else
72
+ primary_key(klass || self.klass)
73
+ end
74
+ end
75
+ end
76
+
77
+ class ThroughReflection < AbstractReflection #:nodoc:
78
+ def association_primary_key(klass = nil)
79
+ # Get the "actual" source reflection if the immediate source reflection has a
80
+ # source reflection itself
81
+ if primary_key = actual_source_reflection.options[:primary_key]
82
+ # CPK
83
+ # @association_primary_key ||= -primary_key.to_s
84
+ @association_primary_key ||= primary_key.freeze
85
+ else
86
+ primary_key(klass || self.klass)
87
+ end
88
+ end
89
+ end
28
90
  end
29
91
  end
@@ -1,12 +1,16 @@
1
1
  module CompositePrimaryKeys
2
2
  module ActiveRecord
3
3
  module Batches
4
- def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
4
+ def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil, order: :asc)
5
5
  relation = self
6
6
  unless block_given?
7
7
  return ::ActiveRecord::Batches::BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
8
8
  end
9
9
 
10
+ unless [:asc, :desc].include?(order)
11
+ raise ArgumentError, ":order must be :asc or :desc, got #{order.inspect}"
12
+ end
13
+
10
14
  if arel.orders.present?
11
15
  act_on_ignored_order(error_on_ignore)
12
16
  end
@@ -17,8 +21,8 @@ module CompositePrimaryKeys
17
21
  batch_limit = remaining if remaining < batch_limit
18
22
  end
19
23
 
20
- relation = relation.reorder(batch_order).limit(batch_limit)
21
- relation = apply_limits(relation, start, finish)
24
+ relation = relation.reorder(batch_order(order)).limit(batch_limit)
25
+ relation = apply_limits(relation, start, finish, order)
22
26
  relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
23
27
  batch_relation = relation
24
28
 
@@ -61,7 +65,9 @@ module CompositePrimaryKeys
61
65
  end
62
66
 
63
67
  # CPK
64
- # batch_relation = relation.where(arel_attribute(primary_key).gt(primary_key_offset))
68
+ #batch_relation = relation.where(
69
+ # predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
70
+ #)
65
71
  batch_relation = if composite?
66
72
  # CPK
67
73
  # Lexicographically select records
@@ -81,7 +87,9 @@ module CompositePrimaryKeys
81
87
  end.reduce(:or)
82
88
  relation.where(query)
83
89
  else
84
- relation.where(arel_attribute(primary_key).gt(primary_key_offset))
90
+ batch_relation = relation.where(
91
+ predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
92
+ )
85
93
  end
86
94
  end
87
95
  end
@@ -95,9 +103,9 @@ module CompositePrimaryKeys
95
103
  ary.length.times.reduce([]) { |results, i| results << ary[0..i] }
96
104
  end
97
105
 
98
- def batch_order
106
+ def batch_order(order)
99
107
  self.primary_key.map do |key|
100
- arel_attribute(key).asc
108
+ table[key].public_send(order)
101
109
  end
102
110
  end
103
111
  end
@@ -1,81 +1,104 @@
1
- module CompositePrimaryKeys
2
- module ActiveRecord
3
- module Calculations
4
- def aggregate_column(column_name)
5
- # CPK
6
- if column_name.kind_of?(Array)
7
- column_name.map do |column|
8
- @klass.arel_attribute(column_name)
9
- end
10
- elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
11
- @klass.arel_attribute(column_name)
12
- else
13
- Arel.sql(column_name == :all ? "*" : column_name.to_s)
14
- end
15
- end
16
-
17
- def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
18
- column_alias = column_name
19
-
20
- # CPK
21
- # if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
22
- # # Shortcut when limit is zero.
23
- # return 0 if limit_value == 0
24
- #
25
- # query_builder = build_count_subquery(spawn, column_name, distinct)
26
- if operation == "count"
27
- relation = unscope(:order)
28
- query_builder = build_count_subquery(spawn, column_name, distinct)
29
- else
30
- # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
31
- relation = unscope(:order).distinct!(false)
32
-
33
- column = aggregate_column(column_name)
34
-
35
- select_value = operation_over_aggregate_column(column, operation, distinct)
36
- if operation == "sum" && distinct
37
- select_value.distinct = true
38
- end
39
-
40
- column_alias = select_value.alias
41
- column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
42
- relation.select_values = [select_value]
43
-
44
- query_builder = relation.arel
45
- end
46
-
47
- result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder, nil) }
48
- row = result.first
49
- value = row && row.values.first
50
- type = result.column_types.fetch(column_alias) do
51
- type_for(column_name)
52
- end
53
-
54
- type_cast_calculated_value(value, type, operation)
55
- end
56
-
57
- def build_count_subquery(relation, column_name, distinct)
58
- if column_name == :all
59
- relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
60
- if relation.select_values.first.is_a?(Array)
61
- relation.select_values = relation.select_values.first.map do |column|
62
- Arel::Attribute.new(@klass.unscoped.table, column)
63
- end
64
- end
65
- elsif column_name.is_a?(Array)
66
- relation.select_values = column_name.map do |column|
67
- Arel::Attribute.new(@klass.unscoped.table, column)
68
- end
69
- else
70
- column_alias = Arel.sql("count_column")
71
- relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
72
- end
73
-
74
- subquery = relation.arel.as(Arel.sql("subquery_for_count"))
75
- select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
76
-
77
- Arel::SelectManager.new(subquery).project(select_value)
78
- end
79
- end
80
- end
81
- end
1
+ module CompositePrimaryKeys
2
+ module ActiveRecord
3
+ module Calculations
4
+ def aggregate_column(column_name)
5
+ # CPK
6
+ if column_name.kind_of?(Array)
7
+ # Note: Test don't seem to run this code?
8
+ column_name.map do |column|
9
+ @klass.arel_table[column]
10
+ end
11
+ elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
12
+ @klass.arel_table[column_name]
13
+ else
14
+ Arel.sql(column_name == :all ? "*" : column_name.to_s)
15
+ end
16
+ end
17
+
18
+ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
19
+ column_alias = column_name
20
+
21
+ # CPK
22
+ # if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
23
+ # # Shortcut when limit is zero.
24
+ # return 0 if limit_value == 0
25
+ #
26
+ # query_builder = build_count_subquery(spawn, column_name, distinct)
27
+ if operation == "count"
28
+ relation = unscope(:order)
29
+ query_builder = build_count_subquery(spawn, column_name, distinct)
30
+ else
31
+ # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
32
+ relation = unscope(:order).distinct!(false)
33
+
34
+ column = aggregate_column(column_name)
35
+ select_value = operation_over_aggregate_column(column, operation, distinct)
36
+ select_value.distinct = true if operation == "sum" && distinct
37
+
38
+ relation.select_values = [select_value]
39
+
40
+ query_builder = relation.arel
41
+ end
42
+
43
+ result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder) }
44
+
45
+ type_cast_calculated_value(result.cast_values.first, operation) do |value|
46
+ type = column.try(:type_caster) ||
47
+ # CPK
48
+ # lookup_cast_type_from_join_dependencies(column_name.to_s) || Type.default_value
49
+ lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
50
+ type.deserialize(value)
51
+ end
52
+ end
53
+
54
+ def build_count_subquery(relation, column_name, distinct)
55
+ if column_name == :all
56
+ column_alias = Arel.star
57
+ # CPK
58
+ # relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
59
+ relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
60
+ elsif column_name.is_a?(Array)
61
+ column_alias = Arel.star
62
+ relation.select_values = column_name.map do |column|
63
+ Arel::Attribute.new(@klass.unscoped.table, column)
64
+ end
65
+ else
66
+ column_alias = Arel.sql("count_column")
67
+ relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
68
+ end
69
+
70
+ subquery_alias = Arel.sql("subquery_for_count")
71
+ select_value = operation_over_aggregate_column(column_alias, "count", false)
72
+
73
+ relation.build_subquery(subquery_alias, select_value)
74
+ end
75
+
76
+ def calculate(operation, column_name)
77
+ if has_include?(column_name)
78
+ relation = apply_join_dependency
79
+
80
+ if operation.to_s.downcase == "count"
81
+ unless distinct_value || distinct_select?(column_name || select_for_count)
82
+ relation.distinct!
83
+ # CPK
84
+ # relation.select_values = [ klass.primary_key || table[Arel.star] ]
85
+ if klass.primary_key.present? && klass.primary_key.is_a?(Array)
86
+ relation.select_values = klass.primary_key.map do |k|
87
+ "#{connection.quote_table_name(klass.table_name)}.#{connection.quote_column_name(k)}"
88
+ end
89
+ else
90
+ relation.select_values = [ klass.primary_key || table[Arel.star] ]
91
+ end
92
+ end
93
+ # PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
94
+ relation.order_values = [] if group_values.empty?
95
+ end
96
+
97
+ relation.calculate(operation, column_name)
98
+ else
99
+ perform_calculation(operation, column_name)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -26,12 +26,12 @@ module CompositePrimaryKeys
26
26
  def limited_ids_for(relation)
27
27
  # CPK
28
28
  # values = @klass.connection.columns_for_distinct(
29
- # connection.column_name_from_arel_node(arel_attribute(primary_key)),
29
+ # connection.column_name_from_arel_node(table[primary_key]),
30
30
  # relation.order_values
31
31
  # )
32
32
 
33
33
  columns = @klass.primary_keys.map do |key|
34
- connection.visitor.compile(arel_attribute(key))
34
+ connection.visitor.compile(table[key])
35
35
  end
36
36
  values = @klass.connection.columns_for_distinct(columns, relation.order_values)
37
37
 
@@ -110,12 +110,12 @@ module CompositePrimaryKeys
110
110
  #
111
111
  # result = limit(limit || 1)
112
112
  # # CPK
113
- # # result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
113
+ # # result.order!(table[primary_key]) if order_values.empty? && primary_key
114
114
  # if order_values.empty? && primary_key
115
115
  # if composite?
116
- # result.order!(primary_keys.map { |pk| arel_attribute(pk).asc })
116
+ # result.order!(primary_keys.map { |pk| table[pk].asc })
117
117
  # elsif
118
- # result.order!(arel_attribute(primary_key))
118
+ # result.order!(table[primary_key])
119
119
  # end
120
120
  # end
121
121
  #
@@ -224,8 +224,8 @@ module CompositePrimaryKeys
224
224
  def ordered_relation
225
225
  if order_values.empty? && (implicit_order_column || primary_key)
226
226
  # CPK
227
- # order(arel_attribute(implicit_order_column || primary_key).asc)
228
- order(Array(implicit_order_column || primary_key).map {|key| arel_attribute(key).asc})
227
+ # order(table[implicit_order_column || primary_key].asc)
228
+ order(Array(implicit_order_column || primary_key).map {|key| table[key].asc})
229
229
  else
230
230
  self
231
231
  end
@@ -3,16 +3,35 @@ module ActiveRecord
3
3
  class AssociationQueryValue
4
4
  def queries
5
5
  # CPK
6
- if associated_table.association_join_foreign_key.is_a?(Array)
6
+ if associated_table.join_foreign_key.is_a?(Array)
7
7
  if ids.is_a?(ActiveRecord::Relation)
8
8
  ids.map do |id|
9
- associated_table.association_join_foreign_key.zip(id.id).to_h
9
+ associated_table.join_foreign_key.zip(id.id).to_h
10
10
  end
11
11
  else
12
- [associated_table.association_join_foreign_key.zip(ids).to_h]
12
+ [associated_table.join_foreign_key.zip(ids).to_h]
13
13
  end
14
14
  else
15
- [associated_table.association_join_foreign_key.to_s => ids]
15
+ [associated_table.join_foreign_key => ids]
16
+ end
17
+ end
18
+
19
+ def ids
20
+ case value
21
+ when Relation
22
+ value.select_values.empty? ? value.select(primary_key) : value
23
+ when Array
24
+ value.map { |v| convert_to_id(v) }
25
+ else
26
+ # CPK
27
+ # convert_to_id(value)
28
+ if value.nil?
29
+ nil
30
+ elsif value.respond_to?(:composite?) && value.composite?
31
+ value.class.primary_keys.zip(value.id)
32
+ else
33
+ convert_to_id(value)
34
+ end
16
35
  end
17
36
  end
18
37
  end
@@ -4,12 +4,12 @@ module CompositePrimaryKeys
4
4
  def reverse_sql_order(order_query)
5
5
  if order_query.empty?
6
6
  # CPK
7
- # return [arel_attribute(primary_key).desc] if primary_key
7
+ # return [table[primary_key].desc] if primary_key
8
8
 
9
9
  if primary_key
10
10
  # break apart CPKs
11
11
  return primary_key.map do |key|
12
- arel_attribute(key).desc
12
+ table[key].desc
13
13
  end
14
14
  else
15
15
  raise IrreversibleOrderError,
@@ -1,23 +1,18 @@
1
1
  module ActiveRecord
2
2
  class Relation
3
3
  class WhereClause
4
- def to_h(table_name = nil)
5
- equalities = equalities(predicates)
4
+ def to_h(table_name = nil, equality_only: false)
5
+ equalities = equalities(predicates, equality_only)
6
6
 
7
7
  # CPK Adds this line, because ours are coming in with AND->{EQUALITY, EQUALITY}
8
8
  equalities = predicates.grep(Arel::Nodes::And).map(&:children).flatten.grep(Arel::Nodes::Equality) if equalities.empty?
9
9
 
10
- if table_name
11
- equalities = equalities.select do |node|
12
- node.left.relation.name == table_name
13
- end
14
- end
15
-
16
- equalities.map { |node|
10
+ equalities.each_with_object({}) do |node, hash|
11
+ next if table_name&.!= node.left.relation.name
17
12
  name = node.left.name.to_s
18
13
  value = extract_node_value(node.right)
19
- [name, value]
20
- }.to_h
14
+ hash[name] = value
15
+ end
21
16
  end
22
17
  end
23
18
  end