composite_primary_keys 14.0.1 → 14.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,110 +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
- # 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
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
@@ -1,8 +1,8 @@
1
- module CompositePrimaryKeys
2
- module VERSION
3
- MAJOR = 14
4
- MINOR = 0
5
- TINY = 1
6
- STRING = [MAJOR, MINOR, TINY].join('.')
7
- end
8
- end
1
+ module CompositePrimaryKeys
2
+ module VERSION
3
+ MAJOR = 14
4
+ MINOR = 0
5
+ TINY = 2
6
+ STRING = [MAJOR, MINOR, TINY].join('.')
7
+ end
8
+ end
@@ -1,119 +1,119 @@
1
- #--
2
- # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- unless defined?(ActiveRecord)
25
- require 'rubygems'
26
- gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
- require 'active_record'
28
- end
29
-
30
- # ActiveModel files we override
31
- # _write_attribute
32
- require 'active_model/attribute_assignment'
33
-
34
- # ActiveRecord files we override
35
- require 'active_record/attribute_methods'
36
- require 'active_record/autosave_association'
37
- require 'active_record/counter_cache'
38
- require 'active_record/fixtures'
39
- require 'active_record/model_schema'
40
- require 'active_record/persistence'
41
- require 'active_record/reflection'
42
- require 'active_record/relation'
43
- require 'active_record/sanitization'
44
- require 'active_record/transactions'
45
-
46
- require 'active_record/associations/association'
47
- require 'active_record/associations/association_scope'
48
- require 'active_record/associations/foreign_association'
49
- require 'active_record/associations/has_many_association'
50
- require 'active_record/associations/has_many_through_association'
51
- require 'active_record/associations/join_dependency'
52
- require 'active_record/associations/preloader/association'
53
- require 'active_record/associations/singular_association'
54
- require 'active_record/associations/collection_association'
55
- require 'active_record/associations/through_association'
56
-
57
- require 'active_record/attribute_methods/primary_key'
58
- require 'active_record/attribute_methods/read'
59
- require 'active_record/attribute_methods/write'
60
- require 'active_record/nested_attributes'
61
-
62
- require 'active_record/connection_adapters/abstract/database_statements'
63
- require 'active_record/connection_adapters/abstract_adapter'
64
- require 'active_record/connection_adapters/postgresql/database_statements'
65
-
66
- require 'active_record/relation/where_clause'
67
- require 'active_record/table_metadata'
68
-
69
- # CPK overrides
70
- require_relative 'composite_primary_keys/active_model/attribute_assignment'
71
- require_relative 'composite_primary_keys/attribute_methods'
72
- require_relative 'composite_primary_keys/autosave_association'
73
- require_relative 'composite_primary_keys/persistence'
74
- require_relative 'composite_primary_keys/base'
75
- require_relative 'composite_primary_keys/core'
76
- require_relative 'composite_primary_keys/composite_arrays'
77
- require_relative 'composite_primary_keys/composite_predicates'
78
- require_relative 'composite_primary_keys/counter_cache'
79
- require_relative 'composite_primary_keys/fixtures'
80
- require_relative 'composite_primary_keys/reflection'
81
- require_relative 'composite_primary_keys/relation'
82
- require_relative 'composite_primary_keys/sanitization'
83
- require_relative 'composite_primary_keys/transactions'
84
- require_relative 'composite_primary_keys/version'
85
-
86
- require_relative 'composite_primary_keys/associations/association'
87
- require_relative 'composite_primary_keys/associations/association_scope'
88
- require_relative 'composite_primary_keys/associations/foreign_association'
89
- require_relative 'composite_primary_keys/associations/has_many_association'
90
- require_relative 'composite_primary_keys/associations/has_many_through_association'
91
- require_relative 'composite_primary_keys/associations/join_dependency'
92
- require_relative 'composite_primary_keys/associations/preloader/association'
93
- require_relative 'composite_primary_keys/associations/collection_association'
94
- require_relative 'composite_primary_keys/associations/through_association'
95
-
96
- require_relative 'composite_primary_keys/attribute_methods/primary_key'
97
- require_relative 'composite_primary_keys/attribute_methods/read'
98
- require_relative 'composite_primary_keys/attribute_methods/write'
99
- require_relative 'composite_primary_keys/nested_attributes'
100
-
101
- require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
102
- require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
103
- require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
104
- require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
105
-
106
- require_relative 'composite_primary_keys/relation/batches'
107
- require_relative 'composite_primary_keys/relation/where_clause'
108
- require_relative 'composite_primary_keys/relation/calculations'
109
- require_relative 'composite_primary_keys/relation/finder_methods'
110
- require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
111
- require_relative 'composite_primary_keys/relation/query_methods'
112
-
113
- require_relative 'composite_primary_keys/validations/uniqueness'
114
-
115
- require_relative 'composite_primary_keys/composite_relation'
116
-
117
- require_relative 'composite_primary_keys/arel/to_sql'
118
- require_relative 'composite_primary_keys/arel/sqlserver'
1
+ #--
2
+ # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ unless defined?(ActiveRecord)
25
+ require 'rubygems'
26
+ gem 'activerecord', '~>7.0.0', '>= 7.0.1'
27
+ require 'active_record'
28
+ end
29
+
30
+ # ActiveModel files we override
31
+ # _write_attribute
32
+ require 'active_model/attribute_assignment'
33
+
34
+ # ActiveRecord files we override
35
+ require 'active_record/attribute_methods'
36
+ require 'active_record/autosave_association'
37
+ require 'active_record/counter_cache'
38
+ require 'active_record/fixtures'
39
+ require 'active_record/model_schema'
40
+ require 'active_record/persistence'
41
+ require 'active_record/reflection'
42
+ require 'active_record/relation'
43
+ require 'active_record/sanitization'
44
+ require 'active_record/transactions'
45
+
46
+ require 'active_record/associations/association'
47
+ require 'active_record/associations/association_scope'
48
+ require 'active_record/associations/foreign_association'
49
+ require 'active_record/associations/has_many_association'
50
+ require 'active_record/associations/has_many_through_association'
51
+ require 'active_record/associations/join_dependency'
52
+ require 'active_record/associations/preloader/association'
53
+ require 'active_record/associations/singular_association'
54
+ require 'active_record/associations/collection_association'
55
+ require 'active_record/associations/through_association'
56
+
57
+ require 'active_record/attribute_methods/primary_key'
58
+ require 'active_record/attribute_methods/read'
59
+ require 'active_record/attribute_methods/write'
60
+ require 'active_record/nested_attributes'
61
+
62
+ require 'active_record/connection_adapters/abstract/database_statements'
63
+ require 'active_record/connection_adapters/abstract_adapter'
64
+ require 'active_record/connection_adapters/postgresql/database_statements'
65
+
66
+ require 'active_record/relation/where_clause'
67
+ require 'active_record/table_metadata'
68
+
69
+ # CPK overrides
70
+ require_relative 'composite_primary_keys/active_model/attribute_assignment'
71
+ require_relative 'composite_primary_keys/attribute_methods'
72
+ require_relative 'composite_primary_keys/autosave_association'
73
+ require_relative 'composite_primary_keys/persistence'
74
+ require_relative 'composite_primary_keys/base'
75
+ require_relative 'composite_primary_keys/core'
76
+ require_relative 'composite_primary_keys/composite_arrays'
77
+ require_relative 'composite_primary_keys/composite_predicates'
78
+ require_relative 'composite_primary_keys/counter_cache'
79
+ require_relative 'composite_primary_keys/fixtures'
80
+ require_relative 'composite_primary_keys/reflection'
81
+ require_relative 'composite_primary_keys/relation'
82
+ require_relative 'composite_primary_keys/sanitization'
83
+ require_relative 'composite_primary_keys/transactions'
84
+ require_relative 'composite_primary_keys/version'
85
+
86
+ require_relative 'composite_primary_keys/associations/association'
87
+ require_relative 'composite_primary_keys/associations/association_scope'
88
+ require_relative 'composite_primary_keys/associations/foreign_association'
89
+ require_relative 'composite_primary_keys/associations/has_many_association'
90
+ require_relative 'composite_primary_keys/associations/has_many_through_association'
91
+ require_relative 'composite_primary_keys/associations/join_association'
92
+ require_relative 'composite_primary_keys/associations/preloader/association'
93
+ require_relative 'composite_primary_keys/associations/collection_association'
94
+ require_relative 'composite_primary_keys/associations/through_association'
95
+
96
+ require_relative 'composite_primary_keys/attribute_methods/primary_key'
97
+ require_relative 'composite_primary_keys/attribute_methods/read'
98
+ require_relative 'composite_primary_keys/attribute_methods/write'
99
+ require_relative 'composite_primary_keys/nested_attributes'
100
+
101
+ require_relative 'composite_primary_keys/connection_adapters/abstract/database_statements'
102
+ require_relative 'composite_primary_keys/connection_adapters/abstract_adapter'
103
+ require_relative 'composite_primary_keys/connection_adapters/postgresql/database_statements'
104
+ require_relative 'composite_primary_keys/connection_adapters/sqlserver/database_statements'
105
+
106
+ require_relative 'composite_primary_keys/relation/batches'
107
+ require_relative 'composite_primary_keys/relation/where_clause'
108
+ require_relative 'composite_primary_keys/relation/calculations'
109
+ require_relative 'composite_primary_keys/relation/finder_methods'
110
+ require_relative 'composite_primary_keys/relation/predicate_builder/association_query_value'
111
+ require_relative 'composite_primary_keys/relation/query_methods'
112
+
113
+ require_relative 'composite_primary_keys/validations/uniqueness'
114
+
115
+ require_relative 'composite_primary_keys/composite_relation'
116
+
117
+ require_relative 'composite_primary_keys/arel/to_sql'
118
+ require_relative 'composite_primary_keys/arel/sqlserver'
119
119
  require_relative 'composite_primary_keys/table_metadata'