composite_primary_keys 12.0.2 → 12.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +880 -841
  3. data/README.rdoc +180 -179
  4. data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -0
  5. data/lib/composite_primary_keys/arel/sqlserver.rb +1 -3
  6. data/lib/composite_primary_keys/associations/association_scope.rb +68 -68
  7. data/lib/composite_primary_keys/associations/join_dependency.rb +103 -103
  8. data/lib/composite_primary_keys/associations/through_association.rb +2 -1
  9. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -0
  10. data/lib/composite_primary_keys/attribute_methods/read.rb +30 -30
  11. data/lib/composite_primary_keys/attribute_methods/write.rb +35 -35
  12. data/lib/composite_primary_keys/attribute_methods.rb +9 -9
  13. data/lib/composite_primary_keys/base.rb +141 -130
  14. data/lib/composite_primary_keys/composite_arrays.rb +0 -8
  15. data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -17
  16. data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -23
  17. data/lib/composite_primary_keys/core.rb +48 -48
  18. data/lib/composite_primary_keys/persistence.rb +82 -81
  19. data/lib/composite_primary_keys/reflection.rb +29 -29
  20. data/lib/composite_primary_keys/relation/batches.rb +1 -1
  21. data/lib/composite_primary_keys/relation/calculations.rb +81 -81
  22. data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
  23. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +20 -20
  24. data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
  25. data/lib/composite_primary_keys/relation/where_clause.rb +23 -23
  26. data/lib/composite_primary_keys/relation.rb +193 -118
  27. data/lib/composite_primary_keys/version.rb +8 -8
  28. data/lib/composite_primary_keys.rb +117 -118
  29. data/test/abstract_unit.rb +114 -113
  30. data/test/connections/databases.ci.yml +22 -19
  31. data/test/fixtures/article.rb +4 -0
  32. data/test/fixtures/articles.yml +4 -3
  33. data/test/fixtures/comment.rb +1 -3
  34. data/test/fixtures/comments.yml +10 -9
  35. data/test/fixtures/db_definitions/db2-create-tables.sql +112 -126
  36. data/test/fixtures/db_definitions/db2-drop-tables.sql +17 -19
  37. data/test/fixtures/db_definitions/mysql.sql +180 -217
  38. data/test/fixtures/db_definitions/oracle.drop.sql +42 -48
  39. data/test/fixtures/db_definitions/oracle.sql +200 -236
  40. data/test/fixtures/db_definitions/postgresql.sql +183 -220
  41. data/test/fixtures/db_definitions/sqlite.sql +170 -206
  42. data/test/fixtures/db_definitions/sqlserver.sql +176 -212
  43. data/test/fixtures/department.rb +16 -11
  44. data/test/fixtures/departments.yml +15 -15
  45. data/test/fixtures/employees.yml +27 -27
  46. data/test/fixtures/readings.yml +2 -2
  47. data/test/fixtures/restaurants_suburbs.yml +11 -11
  48. data/test/fixtures/streets.yml +16 -16
  49. data/test/fixtures/suburbs.yml +14 -14
  50. data/test/fixtures/user.rb +11 -10
  51. data/test/test_associations.rb +358 -351
  52. data/test/test_attributes.rb +60 -60
  53. data/test/test_calculations.rb +42 -42
  54. data/test/test_create.rb +218 -183
  55. data/test/test_delete.rb +182 -179
  56. data/test/test_exists.rb +39 -39
  57. data/test/test_find.rb +164 -145
  58. data/test/test_habtm.rb +2 -2
  59. data/test/test_ids.rb +112 -116
  60. data/test/test_nested_attributes.rb +67 -124
  61. data/test/test_polymorphic.rb +29 -13
  62. data/test/test_preload.rb +4 -3
  63. data/test/test_serialize.rb +2 -2
  64. data/test/test_update.rb +96 -78
  65. metadata +4 -19
  66. data/test/fixtures/hack.rb +0 -5
  67. data/test/fixtures/hacks.yml +0 -3
  68. data/test/fixtures/pk_called_id.rb +0 -5
  69. data/test/fixtures/pk_called_ids.yml +0 -11
  70. data/test/fixtures/reference_code_using_composite_key_alias.rb +0 -8
  71. data/test/fixtures/reference_code_using_simple_key_alias.rb +0 -8
  72. data/test/fixtures/seat.rb +0 -5
  73. data/test/fixtures/seats.yml +0 -9
  74. data/test/fixtures/topic.rb +0 -6
  75. data/test/fixtures/topic_source.rb +0 -7
  76. data/test/test_aliases.rb +0 -18
  77. data/test/test_enum.rb +0 -21
  78. data/test/test_suite.rb +0 -35
@@ -49,14 +49,6 @@ module CompositePrimaryKeys
49
49
  end
50
50
  end
51
51
 
52
- def in(other)
53
- case other
54
- when Arel::SelectManager
55
- Arel::Nodes::In.new(self, other.ast)
56
- end
57
- end
58
-
59
-
60
52
  def to_s
61
53
  # Doing this makes it easier to parse Base#[](attr_name)
62
54
  map { |key| Utils.escape_string_key(key.to_s) }.join(ID_SEP)
@@ -1,17 +1,37 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module DatabaseStatements
4
- def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
5
- sql, binds = to_sql_and_binds(arel, binds)
6
- value = exec_insert(sql, name, binds, pk, sequence_name)
7
-
8
- # CPK
9
- if value && pk.is_a?(Array)
10
- id_value || value.rows.first
11
- else
12
- id_value || last_inserted_id(value)
13
- end
14
- end
15
- end
16
- end
17
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module DatabaseStatements
4
+ def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
5
+ sql, binds = to_sql_and_binds(arel, binds)
6
+ value = exec_insert(sql, name, binds, pk, sequence_name)
7
+
8
+ return id_value if id_value
9
+
10
+ if pk.is_a?(Array) && !value.empty?
11
+ # This is a CPK model and the query result is not empty. Thus we can figure out the new ids for each
12
+ # auto incremented field
13
+ pk.map {|key| value.first[key]}
14
+ elsif pk.is_a?(Array)
15
+ # This is CPK, but we don't know what autoincremented fields were updated.
16
+ result = Array.new(pk.size)
17
+
18
+ # Is there an autoincrementing field?
19
+ auto_key = pk.find do |key|
20
+ attribute = arel.ast.relation[key]
21
+ column = column_for_attribute(attribute)
22
+ if column.respond_to?(:auto_increment?)
23
+ column.auto_increment?
24
+ end
25
+ end
26
+
27
+ if auto_key
28
+ result[pk.index(auto_key)] = last_inserted_id(value)
29
+ end
30
+ result
31
+ else
32
+ last_inserted_id(value)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,23 +1,44 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module SQLServer
4
- module DatabaseStatements
5
- def sql_for_insert(sql, pk, binds)
6
- sql = if pk && self.class.use_output_inserted
7
- # CPK
8
- # quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
9
- # sql.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
10
- quoted_pks = [pk].flatten.map {|pk| "INSERTED.#{SQLServer::Utils.extract_identifiers(pk).quoted}"}
11
- sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT #{quoted_pks.join(", ")}"
12
- else
13
- "#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
14
- end
15
-
16
- # CPK
17
- # super
18
- [sql, binds]
19
- end
20
- end
21
- end
22
- end
23
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module SQLServer
4
+ module DatabaseStatements
5
+ def sql_for_insert(sql, pk, binds)
6
+ if pk.nil?
7
+ table_name = query_requires_identity_insert?(sql)
8
+ pk = primary_key(table_name)
9
+ end
10
+
11
+ sql = if pk && use_output_inserted? && !database_prefix_remote_server?
12
+ # CPK
13
+ #quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
14
+ quoted_pk = Array(pk).map {|subkey| SQLServer::Utils.extract_identifiers(subkey).quoted}
15
+
16
+ table_name ||= get_table_name(sql)
17
+ exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
18
+ if exclude_output_inserted
19
+ id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
20
+ # CPK
21
+ # <<~SQL.squish
22
+ # DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
23
+ # #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
24
+ # SELECT CAST(#{quoted_pk.join(',')} AS #{id_sql_type}) FROM @ssaIdInsertTable
25
+ # SQL
26
+ <<~SQL.squish
27
+ DECLARE @ssaIdInsertTable table (#{quoted_pk.map {|subkey| "#{subkey} #{id_sql_type}"}.join(", ")});
28
+ #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')} INTO @ssaIdInsertTable"}
29
+ SELECT #{quoted_pk.map {|subkey| "CAST(#{subkey} AS #{id_sql_type}) #{subkey}"}.join(", ")} FROM @ssaIdInsertTable
30
+ SQL
31
+ else
32
+ # CPK
33
+ # sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
34
+ sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')}"
35
+ end
36
+ else
37
+ "#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
38
+ end
39
+ super
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,49 +1,49 @@
1
- module ActiveRecord
2
- module Core
3
- def initialize_dup(other) # :nodoc:
4
- @attributes = @attributes.deep_dup
5
- # CPK
6
- #@attributes.reset(@primary_key)
7
- Array(self.class.primary_key).each {|key| @attributes.reset(key)}
8
-
9
- _run_initialize_callbacks
10
-
11
- @new_record = true
12
- @destroyed = false
13
- @_start_transaction_state = nil
14
- @transaction_state = nil
15
-
16
- super
17
- end
18
-
19
- module ClassMethods
20
- def find(*ids) # :nodoc:
21
- # We don't have cache keys for this stuff yet
22
- return super unless ids.length == 1
23
- return super if block_given? ||
24
- primary_key.nil? ||
25
- scope_attributes? ||
26
- columns_hash.key?(inheritance_column) && !base_class?
27
-
28
- # CPK
29
- return super if self.composite?
30
-
31
- id = ids.first
32
-
33
- return super if StatementCache.unsupported_value?(id)
34
-
35
- key = primary_key
36
-
37
- statement = cached_find_by_statement(key) { |params|
38
- where(key => params.bind).limit(1)
39
- }
40
-
41
- record = statement.execute([id], connection)&.first
42
- unless record
43
- raise RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)
44
- end
45
- record
46
- end
47
- end
48
- end
1
+ module ActiveRecord
2
+ module Core
3
+ def initialize_dup(other) # :nodoc:
4
+ @attributes = @attributes.deep_dup
5
+ # CPK
6
+ #@attributes.reset(@primary_key)
7
+ Array(self.class.primary_key).each {|key| @attributes.reset(key)}
8
+
9
+ _run_initialize_callbacks
10
+
11
+ @new_record = true
12
+ @destroyed = false
13
+ @_start_transaction_state = nil
14
+ @transaction_state = nil
15
+
16
+ super
17
+ end
18
+
19
+ module ClassMethods
20
+ def find(*ids) # :nodoc:
21
+ # We don't have cache keys for this stuff yet
22
+ return super unless ids.length == 1
23
+ return super if block_given? ||
24
+ primary_key.nil? ||
25
+ scope_attributes? ||
26
+ columns_hash.key?(inheritance_column) && !base_class?
27
+
28
+ # CPK
29
+ return super if self.composite?
30
+
31
+ id = ids.first
32
+
33
+ return super if StatementCache.unsupported_value?(id)
34
+
35
+ key = primary_key
36
+
37
+ statement = cached_find_by_statement(key) { |params|
38
+ where(key => params.bind).limit(1)
39
+ }
40
+
41
+ record = statement.execute([id], connection)&.first
42
+ unless record
43
+ raise ::ActiveRecord::RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)
44
+ end
45
+ record
46
+ end
47
+ end
48
+ end
49
49
  end
@@ -1,81 +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
- id_or_array.each do |id|
14
- # Is the passed in id actually a record?
15
- id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
16
- delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
17
- end
18
- else
19
- delete_by(primary_key => id_or_array)
20
- end
21
- end
22
-
23
- def _update_record(values, constraints) # :nodoc:
24
- # CPK
25
- if self.composite? && constraints[primary_key]
26
- primary_key_values = constraints.delete(primary_key)
27
- primary_key.each_with_index do |key, i|
28
- constraints[key] = primary_key_values[i]
29
- end
30
- end
31
-
32
- constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
33
-
34
- um = arel_table.where(
35
- constraints.reduce(&:and)
36
- ).compile_update(_substitute_values(values), primary_key)
37
-
38
- connection.update(um, "#{self} Update")
39
- end
40
-
41
- def _delete_record(constraints) # :nodoc:
42
- # CPK
43
- if self.composite? && constraints[primary_key]
44
- primary_key_values = constraints.delete(primary_key)
45
- primary_key.each_with_index do |key, i|
46
- constraints[key] = primary_key_values[i]
47
- end
48
- end
49
-
50
- constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
51
-
52
- dm = Arel::DeleteManager.new
53
- dm.from(arel_table)
54
- dm.wheres = constraints
55
-
56
- connection.delete(dm, "#{self} Destroy")
57
- end
58
- end
59
-
60
- def _create_record(attribute_names = self.attribute_names)
61
- attribute_names = attributes_for_create(attribute_names)
62
-
63
- new_id = self.class._insert_record(
64
- attributes_with_values(attribute_names)
65
- )
66
-
67
- # CPK
68
- if self.composite? && self.id.compact.empty?
69
- self.id = self.id.zip(Array(new_id)).map {|id1, id2| (id1 || id2)}
70
- else
71
- self.id ||= new_id if self.class.primary_key
72
- end
73
-
74
- @new_record = false
75
-
76
- yield(self) if block_given?
77
-
78
- id
79
- end
80
- end
81
- 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
@@ -1,29 +1,29 @@
1
- module ActiveRecord
2
- module Reflection
3
- class AbstractReflection
4
- def join_scope(table, foreign_table, foreign_klass)
5
- predicate_builder = predicate_builder(table)
6
- scope_chain_items = join_scopes(table, predicate_builder)
7
- klass_scope = klass_join_scope(table, predicate_builder)
8
-
9
- key = join_keys.key
10
- foreign_key = join_keys.foreign_key
11
-
12
- # CPK
13
- #klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
14
- constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
15
- klass_scope.where!(constraint)
16
-
17
- if type
18
- klass_scope.where!(type => foreign_klass.polymorphic_name)
19
- end
20
-
21
- if klass.finder_needs_type_condition?
22
- klass_scope.where!(klass.send(:type_condition, table))
23
- end
24
-
25
- scope_chain_items.inject(klass_scope, &:merge!)
26
- end
27
- end
28
- end
29
- end
1
+ module ActiveRecord
2
+ module Reflection
3
+ class AbstractReflection
4
+ def join_scope(table, foreign_table, foreign_klass)
5
+ predicate_builder = predicate_builder(table)
6
+ scope_chain_items = join_scopes(table, predicate_builder)
7
+ klass_scope = klass_join_scope(table, predicate_builder)
8
+
9
+ key = join_keys.key
10
+ foreign_key = join_keys.foreign_key
11
+
12
+ # CPK
13
+ #klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
14
+ constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
15
+ klass_scope.where!(constraint)
16
+
17
+ if type
18
+ klass_scope.where!(type => foreign_klass.polymorphic_name)
19
+ end
20
+
21
+ if klass.finder_needs_type_condition?
22
+ klass_scope.where!(klass.send(:type_condition, table))
23
+ end
24
+
25
+ scope_chain_items.inject(klass_scope, &:merge!)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,7 +4,7 @@ module CompositePrimaryKeys
4
4
  def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
5
5
  relation = self
6
6
  unless block_given?
7
- return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
7
+ return ::ActiveRecord::Batches::BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
8
8
  end
9
9
 
10
10
  if arel.orders.present?
@@ -1,81 +1,81 @@
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
+ 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