composite_primary_keys 12.0.9 → 13.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +15 -1
- data/README.rdoc +1 -0
- 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 → join_association.rb} +137 -103
- 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 +2 -1
- 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 +3 -2
- 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 +46 -23
- 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 +2 -2
- data/lib/composite_primary_keys.rb +4 -2
- 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/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/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 +2 -0
- 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 +14 -0
- 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,193 +1,197 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
class Relation
|
3
|
-
alias :initialize_without_cpk :initialize
|
4
|
-
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
|
5
|
-
initialize_without_cpk(klass, table: table, predicate_builder: predicate_builder, values: values)
|
6
|
-
add_cpk_support if klass && klass.composite?
|
7
|
-
end
|
8
|
-
|
9
|
-
alias :initialize_copy_without_cpk :initialize_copy
|
10
|
-
def initialize_copy(other)
|
11
|
-
initialize_copy_without_cpk(other)
|
12
|
-
add_cpk_support if klass.composite?
|
13
|
-
end
|
14
|
-
|
15
|
-
def add_cpk_support
|
16
|
-
extend CompositePrimaryKeys::CompositeRelation
|
17
|
-
end
|
18
|
-
|
19
|
-
def update_all(updates)
|
20
|
-
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
|
21
|
-
|
22
|
-
if eager_loading?
|
23
|
-
relation = apply_join_dependency
|
24
|
-
return relation.update_all(updates)
|
25
|
-
end
|
26
|
-
|
27
|
-
stmt = Arel::UpdateManager.new
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
stmt
|
34
|
-
stmt.
|
35
|
-
stmt
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
stmt.
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
stmt.set
|
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
|
-
stmt
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
#
|
98
|
-
#
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
arel_table[key]
|
124
|
-
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
#
|
169
|
-
#
|
170
|
-
#
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
subselect.
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
1
|
+
module ActiveRecord
|
2
|
+
class Relation
|
3
|
+
alias :initialize_without_cpk :initialize
|
4
|
+
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
|
5
|
+
initialize_without_cpk(klass, table: table, predicate_builder: predicate_builder, values: values)
|
6
|
+
add_cpk_support if klass && klass.composite?
|
7
|
+
end
|
8
|
+
|
9
|
+
alias :initialize_copy_without_cpk :initialize_copy
|
10
|
+
def initialize_copy(other)
|
11
|
+
initialize_copy_without_cpk(other)
|
12
|
+
add_cpk_support if klass.composite?
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_cpk_support
|
16
|
+
extend CompositePrimaryKeys::CompositeRelation
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_all(updates)
|
20
|
+
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
|
21
|
+
|
22
|
+
if eager_loading?
|
23
|
+
relation = apply_join_dependency
|
24
|
+
return relation.update_all(updates)
|
25
|
+
end
|
26
|
+
|
27
|
+
stmt = Arel::UpdateManager.new
|
28
|
+
stmt.table(arel.join_sources.empty? ? table : arel.source)
|
29
|
+
stmt.key = table[primary_key]
|
30
|
+
|
31
|
+
# CPK
|
32
|
+
if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
|
33
|
+
stmt = Arel::UpdateManager.new
|
34
|
+
stmt.table(arel_table)
|
35
|
+
cpk_subquery(stmt)
|
36
|
+
else
|
37
|
+
stmt.wheres = arel.constraints
|
38
|
+
end
|
39
|
+
stmt.take(arel.limit)
|
40
|
+
stmt.offset(arel.offset)
|
41
|
+
stmt.order(*arel.orders)
|
42
|
+
|
43
|
+
if updates.is_a?(Hash)
|
44
|
+
if klass.locking_enabled? &&
|
45
|
+
!updates.key?(klass.locking_column) &&
|
46
|
+
!updates.key?(klass.locking_column.to_sym)
|
47
|
+
attr = table[klass.locking_column]
|
48
|
+
updates[attr.name] = _increment_attribute(attr)
|
49
|
+
end
|
50
|
+
stmt.set _substitute_values(updates)
|
51
|
+
else
|
52
|
+
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
53
|
+
end
|
54
|
+
|
55
|
+
@klass.connection.update stmt, "#{@klass} Update All"
|
56
|
+
end
|
57
|
+
|
58
|
+
def delete_all
|
59
|
+
invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
|
60
|
+
value = @values[method]
|
61
|
+
method == :distinct ? value : value&.any?
|
62
|
+
end
|
63
|
+
if invalid_methods.any?
|
64
|
+
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
|
65
|
+
end
|
66
|
+
|
67
|
+
if eager_loading?
|
68
|
+
relation = apply_join_dependency
|
69
|
+
return relation.delete_all
|
70
|
+
end
|
71
|
+
|
72
|
+
stmt = Arel::DeleteManager.new
|
73
|
+
stmt.from(arel.join_sources.empty? ? table : arel.source)
|
74
|
+
stmt.key = table[primary_key]
|
75
|
+
|
76
|
+
# CPK
|
77
|
+
if @klass.composite? && @klass.connection.visitor.compile(stmt.ast) =~ /['"]#{primary_key.to_s}['"]/
|
78
|
+
stmt = Arel::DeleteManager.new
|
79
|
+
stmt.from(arel_table)
|
80
|
+
cpk_subquery(stmt)
|
81
|
+
else
|
82
|
+
stmt.wheres = arel.constraints
|
83
|
+
end
|
84
|
+
|
85
|
+
stmt.take(arel.limit)
|
86
|
+
stmt.offset(arel.offset)
|
87
|
+
stmt.order(*arel.orders)
|
88
|
+
|
89
|
+
affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
|
90
|
+
|
91
|
+
reset
|
92
|
+
affected
|
93
|
+
end
|
94
|
+
|
95
|
+
# CPK
|
96
|
+
def cpk_subquery(stmt)
|
97
|
+
# For update and delete statements we need a way to specify which records should
|
98
|
+
# get updated. By default, Rails creates a nested IN subquery that uses the primary
|
99
|
+
# key. Postgresql, Sqlite, MariaDb and Oracle support IN subqueries with multiple
|
100
|
+
# columns but MySQL and SqlServer do not. Instead SQL server supports EXISTS queries
|
101
|
+
# and MySQL supports obfuscated IN queries. Thus we need to check the type of
|
102
|
+
# database adapter to decide how to proceed.
|
103
|
+
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
104
|
+
cpk_mysql_subquery(stmt)
|
105
|
+
elsif defined?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) && connection.is_a?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
|
106
|
+
cpk_exists_subquery(stmt)
|
107
|
+
else
|
108
|
+
cpk_in_subquery(stmt)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Used by postgresql, sqlite, mariadb and oracle. Example query:
|
113
|
+
#
|
114
|
+
# UPDATE reference_codes
|
115
|
+
# SET ...
|
116
|
+
# WHERE (reference_codes.reference_type_id, reference_codes.reference_code) IN
|
117
|
+
# (SELECT reference_codes.reference_type_id, reference_codes.reference_code
|
118
|
+
# FROM reference_codes)
|
119
|
+
def cpk_in_subquery(stmt)
|
120
|
+
# Setup the subquery
|
121
|
+
subquery = arel.clone
|
122
|
+
subquery.projections = primary_keys.map do |key|
|
123
|
+
arel_table[key]
|
124
|
+
end
|
125
|
+
|
126
|
+
where_fields = primary_keys.map do |key|
|
127
|
+
arel_table[key]
|
128
|
+
end
|
129
|
+
where = Arel::Nodes::Grouping.new(where_fields).in(subquery)
|
130
|
+
stmt.wheres = [where]
|
131
|
+
end
|
132
|
+
|
133
|
+
# CPK. This is an alternative to IN subqueries. It is used by sqlserver.
|
134
|
+
# Example query:
|
135
|
+
#
|
136
|
+
# UPDATE reference_codes
|
137
|
+
# SET ...
|
138
|
+
# WHERE EXISTS
|
139
|
+
# (SELECT 1
|
140
|
+
# FROM reference_codes cpk_child
|
141
|
+
# WHERE reference_codes.reference_type_id = cpk_child.reference_type_id AND
|
142
|
+
# reference_codes.reference_code = cpk_child.reference_code)
|
143
|
+
def cpk_exists_subquery(stmt)
|
144
|
+
arel_attributes = primary_keys.map do |key|
|
145
|
+
table[key]
|
146
|
+
end.to_composite_keys
|
147
|
+
|
148
|
+
# Clone the query
|
149
|
+
subselect = arel.clone
|
150
|
+
|
151
|
+
# Alias the table - we assume just one table
|
152
|
+
aliased_table = subselect.froms.first
|
153
|
+
aliased_table.table_alias = "cpk_child"
|
154
|
+
|
155
|
+
# Project - really we could just set this to "1"
|
156
|
+
subselect.projections = arel_attributes
|
157
|
+
|
158
|
+
# Setup correlation to the outer query via where clauses
|
159
|
+
primary_keys.map do |key|
|
160
|
+
outer_attribute = arel_table[key]
|
161
|
+
inner_attribute = aliased_table[key]
|
162
|
+
where = outer_attribute.eq(inner_attribute)
|
163
|
+
subselect.where(where)
|
164
|
+
end
|
165
|
+
stmt.wheres = [Arel::Nodes::Exists.new(subselect)]
|
166
|
+
end
|
167
|
+
|
168
|
+
# CPK. This is the old way CPK created subqueries and is used by MySql.
|
169
|
+
# MySQL does not support referencing the same table that is being UPDATEd or
|
170
|
+
# DELETEd in a subquery so we obfuscate it. The ugly query looks like this:
|
171
|
+
#
|
172
|
+
# UPDATE `reference_codes`
|
173
|
+
# SET ...
|
174
|
+
# WHERE (reference_codes.reference_type_id, reference_codes.reference_code) IN
|
175
|
+
# (SELECT reference_type_id,reference_code
|
176
|
+
# FROM (SELECT DISTINCT `reference_codes`.`reference_type_id`, `reference_codes`.`reference_code`
|
177
|
+
# FROM `reference_codes`) __active_record_temp)
|
178
|
+
def cpk_mysql_subquery(stmt)
|
179
|
+
arel_attributes = primary_keys.map do |key|
|
180
|
+
table[key]
|
181
|
+
end.to_composite_keys
|
182
|
+
|
183
|
+
subselect = arel.clone
|
184
|
+
subselect.projections = arel_attributes
|
185
|
+
|
186
|
+
# Materialize subquery by adding distinct
|
187
|
+
# to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
|
188
|
+
subselect.distinct unless arel.limit || arel.offset || arel.orders.any?
|
189
|
+
|
190
|
+
key_name = arel_attributes.map(&:name).join(',')
|
191
|
+
|
192
|
+
manager = Arel::SelectManager.new(subselect.as("__active_record_temp")).project(Arel.sql(key_name))
|
193
|
+
|
194
|
+
stmt.wheres = [Arel::Nodes::In.new(arel_attributes, manager.ast)]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
@@ -1,43 +1,43 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Sanitization
|
3
|
-
module ClassMethods
|
4
|
-
# def expand_hash_conditions_for_aggregates(attrs)
|
5
|
-
# expanded_attrs = {}
|
6
|
-
# attrs.each do |attr, value|
|
7
|
-
# # CPK
|
8
|
-
# # if aggregation = reflect_on_aggregation(attr.to_sym)
|
9
|
-
# if attr.is_a?(CompositePrimaryKeys::CompositeKeys)
|
10
|
-
# value = value.split('/') if value.is_a?(String)
|
11
|
-
# attr.each_with_index do |key,i|
|
12
|
-
# expanded_attrs[key] = value.respond_to?(:flatten) ? value.flatten[i] : value
|
13
|
-
# end
|
14
|
-
# elsif aggregation = reflect_on_aggregation(attr.to_sym)
|
15
|
-
# mapping = aggregation.mapping
|
16
|
-
# mapping.each do |field_attr, aggregate_attr|
|
17
|
-
# if mapping.size == 1 && !value.respond_to?(aggregate_attr)
|
18
|
-
# expanded_attrs[field_attr] = value
|
19
|
-
# else
|
20
|
-
# expanded_attrs[field_attr] = value.send(aggregate_attr)
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
# else
|
24
|
-
# expanded_attrs[attr] = value
|
25
|
-
# end
|
26
|
-
# end
|
27
|
-
# expanded_attrs
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# def quoted_id
|
31
|
-
# # CPK
|
32
|
-
# # self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
|
33
|
-
# if self.composite?
|
34
|
-
# [self.class.primary_keys, ids].transpose.map { |attr_name,id|
|
35
|
-
# self.class.quote_value(@attributes[attr_name].value_for_database)
|
36
|
-
# }
|
37
|
-
# else
|
38
|
-
# self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
|
39
|
-
# end
|
40
|
-
# end
|
41
|
-
end
|
42
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Sanitization
|
3
|
+
module ClassMethods
|
4
|
+
# def expand_hash_conditions_for_aggregates(attrs)
|
5
|
+
# expanded_attrs = {}
|
6
|
+
# attrs.each do |attr, value|
|
7
|
+
# # CPK
|
8
|
+
# # if aggregation = reflect_on_aggregation(attr.to_sym)
|
9
|
+
# if attr.is_a?(CompositePrimaryKeys::CompositeKeys)
|
10
|
+
# value = value.split('/') if value.is_a?(String)
|
11
|
+
# attr.each_with_index do |key,i|
|
12
|
+
# expanded_attrs[key] = value.respond_to?(:flatten) ? value.flatten[i] : value
|
13
|
+
# end
|
14
|
+
# elsif aggregation = reflect_on_aggregation(attr.to_sym)
|
15
|
+
# mapping = aggregation.mapping
|
16
|
+
# mapping.each do |field_attr, aggregate_attr|
|
17
|
+
# if mapping.size == 1 && !value.respond_to?(aggregate_attr)
|
18
|
+
# expanded_attrs[field_attr] = value
|
19
|
+
# else
|
20
|
+
# expanded_attrs[field_attr] = value.send(aggregate_attr)
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# else
|
24
|
+
# expanded_attrs[attr] = value
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
# expanded_attrs
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# def quoted_id
|
31
|
+
# # CPK
|
32
|
+
# # self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
|
33
|
+
# if self.composite?
|
34
|
+
# [self.class.primary_keys, ids].transpose.map { |attr_name,id|
|
35
|
+
# self.class.quote_value(@attributes[attr_name].value_for_database)
|
36
|
+
# }
|
37
|
+
# else
|
38
|
+
# self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
end
|
42
|
+
end
|
43
43
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class TableMetadata # :nodoc:
|
5
|
+
def associated_with?(table_name)
|
6
|
+
# CPK
|
7
|
+
# klass&._reflect_on_association(table_name) || klass&._reflect_on_association(table_name.singularize)
|
8
|
+
klass&._reflect_on_association(table_name) || klass&._reflect_on_association(table_name.to_s.singularize)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -1,34 +1,34 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Transactions
|
3
|
-
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
|
4
|
-
def restore_transaction_record_state(force_restore_state = false)
|
5
|
-
if restore_state = @_start_transaction_state
|
6
|
-
if force_restore_state || restore_state[:level] <= 1
|
7
|
-
@new_record = restore_state[:new_record]
|
8
|
-
@destroyed = restore_state[:destroyed]
|
9
|
-
@attributes = restore_state[:attributes].map do |attr|
|
10
|
-
value = @attributes.fetch_value(attr.name)
|
11
|
-
attr = attr.with_value_from_user(value) if attr.value != value
|
12
|
-
attr
|
13
|
-
end
|
14
|
-
@mutations_from_database = nil
|
15
|
-
@mutations_before_last_save = nil
|
16
|
-
|
17
|
-
# CPK
|
18
|
-
if self.composite?
|
19
|
-
values = @primary_key.map {|attribute| @attributes.fetch_value(attribute)}
|
20
|
-
restore_id = restore_state[:id]
|
21
|
-
if values != restore_id
|
22
|
-
@primary_key.each_with_index do |attribute, i|
|
23
|
-
@attributes.write_from_user(attribute, restore_id[i])
|
24
|
-
end
|
25
|
-
end
|
26
|
-
elsif @attributes.fetch_value(@primary_key) != restore_state[:id]
|
27
|
-
@attributes.write_from_user(@primary_key, restore_state[:id])
|
28
|
-
end
|
29
|
-
freeze if restore_state[:frozen?]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Transactions
|
3
|
+
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
|
4
|
+
def restore_transaction_record_state(force_restore_state = false)
|
5
|
+
if restore_state = @_start_transaction_state
|
6
|
+
if force_restore_state || restore_state[:level] <= 1
|
7
|
+
@new_record = restore_state[:new_record]
|
8
|
+
@destroyed = restore_state[:destroyed]
|
9
|
+
@attributes = restore_state[:attributes].map do |attr|
|
10
|
+
value = @attributes.fetch_value(attr.name)
|
11
|
+
attr = attr.with_value_from_user(value) if attr.value != value
|
12
|
+
attr
|
13
|
+
end
|
14
|
+
@mutations_from_database = nil
|
15
|
+
@mutations_before_last_save = nil
|
16
|
+
|
17
|
+
# CPK
|
18
|
+
if self.composite?
|
19
|
+
values = @primary_key.map {|attribute| @attributes.fetch_value(attribute)}
|
20
|
+
restore_id = restore_state[:id]
|
21
|
+
if values != restore_id
|
22
|
+
@primary_key.each_with_index do |attribute, i|
|
23
|
+
@attributes.write_from_user(attribute, restore_id[i])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
elsif @attributes.fetch_value(@primary_key) != restore_state[:id]
|
27
|
+
@attributes.write_from_user(@primary_key, restore_state[:id])
|
28
|
+
end
|
29
|
+
freeze if restore_state[:frozen?]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,32 +1,32 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Validations
|
3
|
-
class UniquenessValidator
|
4
|
-
def validate_each(record, attribute, value)
|
5
|
-
finder_class = find_finder_class_for(record)
|
6
|
-
value = map_enum_attribute(finder_class, attribute, value)
|
7
|
-
|
8
|
-
relation = build_relation(finder_class, attribute, value)
|
9
|
-
if record.persisted?
|
10
|
-
# CPK
|
11
|
-
if finder_class.primary_key.is_a?(Array)
|
12
|
-
predicate = finder_class.cpk_id_predicate(finder_class.arel_table, finder_class.primary_key, record.id_in_database || record.id)
|
13
|
-
relation = relation.where.not(predicate)
|
14
|
-
elsif finder_class.primary_key
|
15
|
-
relation = relation.where.not(finder_class.primary_key => record.id_in_database)
|
16
|
-
else
|
17
|
-
raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
relation = scope_relation(record, relation)
|
21
|
-
relation = relation.merge(options[:conditions]) if options[:conditions]
|
22
|
-
|
23
|
-
if relation.exists?
|
24
|
-
error_options = options.except(:case_sensitive, :scope, :conditions)
|
25
|
-
error_options[:value] = value
|
26
|
-
|
27
|
-
record.errors.add(attribute, :taken, **error_options)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
class UniquenessValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
finder_class = find_finder_class_for(record)
|
6
|
+
value = map_enum_attribute(finder_class, attribute, value)
|
7
|
+
|
8
|
+
relation = build_relation(finder_class, attribute, value)
|
9
|
+
if record.persisted?
|
10
|
+
# CPK
|
11
|
+
if finder_class.primary_key.is_a?(Array)
|
12
|
+
predicate = finder_class.cpk_id_predicate(finder_class.arel_table, finder_class.primary_key, record.id_in_database || record.id)
|
13
|
+
relation = relation.where.not(predicate)
|
14
|
+
elsif finder_class.primary_key
|
15
|
+
relation = relation.where.not(finder_class.primary_key => record.id_in_database)
|
16
|
+
else
|
17
|
+
raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
relation = scope_relation(record, relation)
|
21
|
+
relation = relation.merge(options[:conditions]) if options[:conditions]
|
22
|
+
|
23
|
+
if relation.exists?
|
24
|
+
error_options = options.except(:case_sensitive, :scope, :conditions)
|
25
|
+
error_options[:value] = value
|
26
|
+
|
27
|
+
record.errors.add(attribute, :taken, **error_options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
32
|
end
|