composite_primary_keys 12.0.9 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.rdoc +894 -877
- data/README.rdoc +182 -180
- data/Rakefile +37 -37
- data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -19
- data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
- data/lib/composite_primary_keys/arel/to_sql.rb +18 -18
- data/lib/composite_primary_keys/associations/association.rb +23 -23
- data/lib/composite_primary_keys/associations/association_scope.rb +66 -68
- data/lib/composite_primary_keys/associations/collection_association.rb +31 -31
- data/lib/composite_primary_keys/associations/foreign_association.rb +15 -15
- data/lib/composite_primary_keys/associations/has_many_association.rb +35 -35
- data/lib/composite_primary_keys/associations/join_dependency.rb +137 -103
- data/lib/composite_primary_keys/associations/preloader/association.rb +68 -53
- data/lib/composite_primary_keys/associations/through_association.rb +25 -25
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +0 -2
- data/lib/composite_primary_keys/attribute_methods/read.rb +30 -30
- data/lib/composite_primary_keys/attribute_methods/write.rb +35 -35
- data/lib/composite_primary_keys/attribute_methods.rb +21 -9
- data/lib/composite_primary_keys/autosave_association.rb +60 -60
- data/lib/composite_primary_keys/base.rb +141 -141
- data/lib/composite_primary_keys/composite_arrays.rb +86 -86
- data/lib/composite_primary_keys/composite_predicates.rb +71 -69
- data/lib/composite_primary_keys/composite_relation.rb +29 -29
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -37
- data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
- data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
- data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -44
- data/lib/composite_primary_keys/core.rb +48 -48
- data/lib/composite_primary_keys/counter_cache.rb +15 -15
- data/lib/composite_primary_keys/fixtures.rb +21 -21
- data/lib/composite_primary_keys/nested_attributes.rb +1 -1
- data/lib/composite_primary_keys/persistence.rb +96 -81
- data/lib/composite_primary_keys/reflection.rb +91 -29
- data/lib/composite_primary_keys/relation/batches.rb +15 -7
- data/lib/composite_primary_keys/relation/calculations.rb +110 -81
- data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +39 -20
- data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
- data/lib/composite_primary_keys/relation/where_clause.rb +18 -23
- data/lib/composite_primary_keys/relation.rb +197 -193
- data/lib/composite_primary_keys/sanitization.rb +42 -42
- data/lib/composite_primary_keys/table_metadata.rb +11 -0
- data/lib/composite_primary_keys/transactions.rb +34 -34
- data/lib/composite_primary_keys/validations/uniqueness.rb +31 -31
- data/lib/composite_primary_keys/version.rb +8 -8
- data/lib/composite_primary_keys.rb +119 -117
- data/scripts/console.rb +48 -48
- data/scripts/txt2html +76 -76
- data/scripts/txt2js +65 -65
- data/tasks/databases/mysql.rake +40 -40
- data/tasks/databases/oracle.rake +41 -41
- data/tasks/databases/postgresql.rake +38 -38
- data/tasks/databases/sqlite.rake +25 -25
- data/tasks/databases/sqlserver.rake +43 -43
- data/tasks/website.rake +18 -18
- data/test/README_tests.rdoc +56 -56
- data/test/abstract_unit.rb +118 -114
- data/test/connections/connection_spec.rb +27 -27
- data/test/connections/databases.ci.yml +22 -22
- data/test/connections/databases.example.yml +40 -40
- data/test/connections/databases.yml +40 -39
- data/test/fixtures/article.rb +10 -10
- data/test/fixtures/articles.yml +7 -7
- data/test/fixtures/capitol.rb +3 -3
- data/test/fixtures/capitols.yml +16 -16
- data/test/fixtures/comment.rb +5 -5
- data/test/fixtures/comments.yml +17 -17
- data/test/fixtures/db_definitions/db2-create-tables.sql +112 -112
- data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -16
- data/test/fixtures/db_definitions/mysql.sql +180 -180
- data/test/fixtures/db_definitions/oracle.drop.sql +41 -41
- data/test/fixtures/db_definitions/oracle.sql +199 -199
- data/test/fixtures/db_definitions/postgresql.sql +182 -182
- data/test/fixtures/db_definitions/sqlite.sql +169 -169
- data/test/fixtures/db_definitions/sqlserver.sql +176 -176
- data/test/fixtures/department.rb +16 -16
- data/test/fixtures/departments.yml +19 -15
- data/test/fixtures/dorm.rb +2 -2
- data/test/fixtures/dorms.yml +4 -4
- data/test/fixtures/employee.rb +5 -5
- data/test/fixtures/employees.yml +33 -28
- data/test/fixtures/group.rb +2 -2
- data/test/fixtures/groups.yml +6 -6
- data/test/fixtures/membership.rb +8 -6
- data/test/fixtures/membership_status.rb +2 -2
- data/test/fixtures/membership_statuses.yml +16 -16
- data/test/fixtures/memberships.yml +10 -10
- data/test/fixtures/product.rb +9 -9
- data/test/fixtures/product_tariff.rb +5 -5
- data/test/fixtures/product_tariffs.yml +14 -14
- data/test/fixtures/products.yml +11 -11
- data/test/fixtures/reading.rb +4 -4
- data/test/fixtures/readings.yml +10 -10
- data/test/fixtures/reference_code.rb +7 -7
- data/test/fixtures/reference_codes.yml +28 -28
- data/test/fixtures/reference_type.rb +12 -12
- data/test/fixtures/reference_types.yml +9 -9
- data/test/fixtures/restaurant.rb +9 -9
- data/test/fixtures/restaurants.yml +14 -14
- data/test/fixtures/restaurants_suburb.rb +2 -2
- data/test/fixtures/restaurants_suburbs.yml +10 -10
- data/test/fixtures/room.rb +11 -11
- data/test/fixtures/room_assignment.rb +13 -13
- data/test/fixtures/room_assignments.yml +24 -24
- data/test/fixtures/room_attribute.rb +2 -2
- data/test/fixtures/room_attribute_assignment.rb +4 -4
- data/test/fixtures/room_attribute_assignments.yml +4 -4
- data/test/fixtures/room_attributes.yml +2 -2
- data/test/fixtures/rooms.yml +12 -12
- data/test/fixtures/street.rb +2 -2
- data/test/fixtures/streets.yml +16 -16
- data/test/fixtures/student.rb +3 -3
- data/test/fixtures/students.yml +15 -15
- data/test/fixtures/suburb.rb +5 -5
- data/test/fixtures/suburbs.yml +14 -14
- data/test/fixtures/tariff.rb +5 -5
- data/test/fixtures/tariffs.yml +14 -14
- data/test/fixtures/topic_sources.yml +3 -3
- data/test/fixtures/topics.yml +8 -8
- data/test/fixtures/user.rb +11 -11
- data/test/fixtures/users.yml +10 -10
- data/test/plugins/pagination.rb +405 -405
- data/test/plugins/pagination_helper.rb +135 -135
- data/test/test_associations.rb +372 -358
- data/test/test_attribute_methods.rb +63 -63
- data/test/test_attributes.rb +75 -60
- data/test/test_calculations.rb +49 -42
- data/test/test_callbacks.rb +99 -99
- data/test/test_composite_arrays.rb +38 -38
- data/test/test_counter_cache.rb +30 -30
- data/test/test_create.rb +218 -206
- data/test/test_delete.rb +188 -179
- data/test/test_dumpable.rb +15 -15
- data/test/test_dup.rb +37 -37
- data/test/test_equal.rb +26 -26
- data/test/test_exists.rb +39 -39
- data/test/test_find.rb +170 -164
- data/test/test_habtm.rb +141 -141
- data/test/test_ids.rb +112 -112
- data/test/test_miscellaneous.rb +32 -32
- data/test/test_nested_attributes.rb +67 -67
- data/test/test_optimistic.rb +18 -18
- data/test/test_pagination.rb +35 -35
- data/test/test_polymorphic.rb +43 -43
- data/test/test_predicates.rb +59 -59
- data/test/test_preload.rb +102 -102
- data/test/test_santiago.rb +23 -23
- data/test/test_touch.rb +23 -23
- data/test/test_tutorial_example.rb +25 -25
- data/test/test_update.rb +102 -96
- data/test/test_validations.rb +13 -13
- metadata +7 -6
|
@@ -1,29 +1,91 @@
|
|
|
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 =
|
|
10
|
-
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
|
-
|
|
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_primary_key
|
|
10
|
+
foreign_key = join_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
|
+
|
|
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
|
|
90
|
+
end
|
|
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
|
-
#
|
|
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(
|
|
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
|
-
|
|
108
|
+
table[key].public_send(order)
|
|
101
109
|
end
|
|
102
110
|
end
|
|
103
111
|
end
|
|
@@ -1,81 +1,110 @@
|
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
select_value = operation_over_aggregate_column(column, operation, distinct)
|
|
36
|
-
if operation == "sum" && distinct
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
# 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
|
+
if operation != "count"
|
|
46
|
+
type = column.try(:type_caster) ||
|
|
47
|
+
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
|
48
|
+
type = type.subtype if ::ActiveRecord::Enum::EnumType === type
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
type_cast_calculated_value(result.cast_values.first, operation, type) do |value|
|
|
52
|
+
type = column.try(:type_caster) ||
|
|
53
|
+
# CPK
|
|
54
|
+
# lookup_cast_type_from_join_dependencies(column_name.to_s) || Type.default_value
|
|
55
|
+
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
|
56
|
+
type.deserialize(value)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def build_count_subquery(relation, column_name, distinct)
|
|
61
|
+
if column_name == :all
|
|
62
|
+
column_alias = Arel.star
|
|
63
|
+
# CPK
|
|
64
|
+
# relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
|
|
65
|
+
relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
|
|
66
|
+
elsif column_name.is_a?(Array)
|
|
67
|
+
column_alias = Arel.star
|
|
68
|
+
relation.select_values = column_name.map do |column|
|
|
69
|
+
Arel::Attribute.new(@klass.unscoped.table, column)
|
|
70
|
+
end
|
|
71
|
+
else
|
|
72
|
+
column_alias = Arel.sql("count_column")
|
|
73
|
+
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
subquery_alias = Arel.sql("subquery_for_count")
|
|
77
|
+
select_value = operation_over_aggregate_column(column_alias, "count", false)
|
|
78
|
+
|
|
79
|
+
relation.build_subquery(subquery_alias, select_value)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def calculate(operation, column_name)
|
|
83
|
+
if has_include?(column_name)
|
|
84
|
+
relation = apply_join_dependency
|
|
85
|
+
|
|
86
|
+
if operation.to_s.downcase == "count"
|
|
87
|
+
unless distinct_value || distinct_select?(column_name || select_for_count)
|
|
88
|
+
relation.distinct!
|
|
89
|
+
# CPK
|
|
90
|
+
# relation.select_values = [ klass.primary_key || table[Arel.star] ]
|
|
91
|
+
if klass.primary_key.present? && klass.primary_key.is_a?(Array)
|
|
92
|
+
relation.select_values = klass.primary_key.map do |k|
|
|
93
|
+
"#{connection.quote_table_name(klass.table_name)}.#{connection.quote_column_name(k)}"
|
|
94
|
+
end
|
|
95
|
+
else
|
|
96
|
+
relation.select_values = [ klass.primary_key || table[Arel.star] ]
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
|
|
100
|
+
relation.order_values = [] if group_values.empty?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
relation.calculate(operation, column_name)
|
|
104
|
+
else
|
|
105
|
+
perform_calculation(operation, column_name)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|