composite_primary_keys 14.0.8 → 14.0.10
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 +13 -0
- data/README.rdoc +182 -182
- data/Rakefile +37 -37
- data/lib/composite_primary_keys/associations/collection_association.rb +38 -38
- data/lib/composite_primary_keys/associations/preloader/association.rb +52 -52
- data/lib/composite_primary_keys/autosave_association.rb +60 -60
- data/lib/composite_primary_keys/composite_arrays.rb +88 -88
- data/lib/composite_primary_keys/composite_predicates.rb +121 -121
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +36 -36
- data/lib/composite_primary_keys/core.rb +71 -48
- data/lib/composite_primary_keys/nested_attributes.rb +2 -2
- data/lib/composite_primary_keys/persistence.rb +96 -96
- data/lib/composite_primary_keys/reflection.rb +93 -91
- data/lib/composite_primary_keys/relation/calculations.rb +110 -110
- data/lib/composite_primary_keys/relation/query_methods.rb +40 -40
- data/lib/composite_primary_keys/relation.rb +199 -199
- data/lib/composite_primary_keys/validations/uniqueness.rb +40 -40
- data/lib/composite_primary_keys/version.rb +1 -1
- data/lib/composite_primary_keys.rb +117 -117
- data/scripts/console.rb +48 -48
- data/tasks/databases/trilogy.rake +23 -23
- data/test/abstract_unit.rb +124 -124
- data/test/connections/databases.ci.yml +32 -32
- data/test/fixtures/admin.rb +4 -4
- data/test/fixtures/db_definitions/db2-create-tables.sql +146 -146
- data/test/fixtures/db_definitions/db2-drop-tables.sql +23 -23
- data/test/fixtures/db_definitions/mysql.sql +203 -203
- data/test/fixtures/db_definitions/oracle.drop.sql +45 -45
- data/test/fixtures/db_definitions/oracle.sql +220 -220
- data/test/fixtures/db_definitions/postgresql.sql +205 -205
- data/test/fixtures/db_definitions/sqlite.sql +190 -190
- data/test/fixtures/db_definitions/sqlserver.sql +199 -199
- data/test/fixtures/department.rb +20 -20
- data/test/fixtures/moderator.rb +4 -4
- data/test/fixtures/room.rb +14 -14
- data/test/fixtures/room_assignment.rb +18 -18
- data/test/fixtures/staff_room.rb +6 -6
- data/test/fixtures/staff_room_key.rb +6 -6
- data/test/fixtures/user.rb +14 -14
- data/test/test_associations.rb +403 -403
- data/test/test_composite_arrays.rb +44 -44
- data/test/test_equal.rb +55 -26
- data/test/test_has_one_through.rb +30 -30
- data/test/test_hash.rb +73 -0
- data/test/test_nested_attributes.rb +90 -67
- metadata +7 -8
@@ -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,41 +1,41 @@
|
|
1
|
-
module CompositePrimaryKeys
|
2
|
-
module ActiveRecord
|
3
|
-
module QueryMethods
|
4
|
-
def reverse_sql_order(order_query)
|
5
|
-
if order_query.empty?
|
6
|
-
# CPK
|
7
|
-
# return [table[primary_key].desc] if primary_key
|
8
|
-
|
9
|
-
if primary_key
|
10
|
-
# break apart CPKs
|
11
|
-
return primary_key.map do |key|
|
12
|
-
table[key].desc
|
13
|
-
end
|
14
|
-
else
|
15
|
-
raise IrreversibleOrderError,
|
16
|
-
"Relation has no current order and table has no primary key to be used as default order"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
order_query.flat_map do |o|
|
21
|
-
case o
|
22
|
-
when Arel::Attribute
|
23
|
-
o.desc
|
24
|
-
when Arel::Nodes::Ordering
|
25
|
-
o.reverse
|
26
|
-
when String
|
27
|
-
if does_not_support_reverse?(o)
|
28
|
-
raise IrreversibleOrderError, "Order #{o.inspect} can not be reversed automatically"
|
29
|
-
end
|
30
|
-
o.split(",").map! do |s|
|
31
|
-
s.strip!
|
32
|
-
s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
|
33
|
-
end
|
34
|
-
else
|
35
|
-
o
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
module ActiveRecord
|
3
|
+
module QueryMethods
|
4
|
+
def reverse_sql_order(order_query)
|
5
|
+
if order_query.empty?
|
6
|
+
# CPK
|
7
|
+
# return [table[primary_key].desc] if primary_key
|
8
|
+
|
9
|
+
if primary_key
|
10
|
+
# break apart CPKs
|
11
|
+
return primary_key.map do |key|
|
12
|
+
table[key].desc
|
13
|
+
end
|
14
|
+
else
|
15
|
+
raise IrreversibleOrderError,
|
16
|
+
"Relation has no current order and table has no primary key to be used as default order"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
order_query.flat_map do |o|
|
21
|
+
case o
|
22
|
+
when Arel::Attribute
|
23
|
+
o.desc
|
24
|
+
when Arel::Nodes::Ordering
|
25
|
+
o.reverse
|
26
|
+
when String
|
27
|
+
if does_not_support_reverse?(o)
|
28
|
+
raise IrreversibleOrderError, "Order #{o.inspect} can not be reversed automatically"
|
29
|
+
end
|
30
|
+
o.split(",").map! do |s|
|
31
|
+
s.strip!
|
32
|
+
s.gsub!(/\sasc\Z/i, " DESC") || s.gsub!(/\sdesc\Z/i, " ASC") || (s << " DESC")
|
33
|
+
end
|
34
|
+
else
|
35
|
+
o
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
41
|
end
|