composite_primary_keys 11.0.0.rc2 → 11.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +774 -763
- data/Rakefile +37 -37
- data/lib/composite_primary_keys.rb +120 -122
- data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
- data/lib/composite_primary_keys/arel/to_sql.rb +32 -32
- data/lib/composite_primary_keys/associations/association.rb +23 -23
- data/lib/composite_primary_keys/associations/association_scope.rb +68 -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 +34 -34
- data/lib/composite_primary_keys/associations/join_dependency.rb +102 -102
- data/lib/composite_primary_keys/associations/preloader/association.rb +117 -117
- data/lib/composite_primary_keys/attribute_methods.rb +9 -9
- data/lib/composite_primary_keys/attribute_methods/read.rb +27 -27
- data/lib/composite_primary_keys/attribute_methods/write.rb +44 -44
- data/lib/composite_primary_keys/autosave_association.rb +32 -32
- data/lib/composite_primary_keys/base.rb +130 -130
- data/lib/composite_primary_keys/composite_arrays.rb +51 -51
- data/lib/composite_primary_keys/composite_predicates.rb +69 -69
- data/lib/composite_primary_keys/composite_relation.rb +29 -29
- data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
- data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +29 -29
- data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
- data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +22 -22
- data/lib/composite_primary_keys/core.rb +54 -54
- data/lib/composite_primary_keys/counter_cache.rb +28 -28
- data/lib/composite_primary_keys/fixtures.rb +26 -26
- data/lib/composite_primary_keys/persistence.rb +120 -112
- data/lib/composite_primary_keys/reflection.rb +20 -20
- data/lib/composite_primary_keys/relation.rb +88 -88
- data/lib/composite_primary_keys/relation/calculations.rb +79 -79
- data/lib/composite_primary_keys/relation/finder_methods.rb +229 -229
- data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +20 -20
- data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
- data/lib/composite_primary_keys/relation/where_clause.rb +24 -24
- data/lib/composite_primary_keys/sanitization.rb +42 -42
- data/lib/composite_primary_keys/validations/uniqueness.rb +32 -32
- data/lib/composite_primary_keys/version.rb +8 -8
- 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 +120 -120
- data/test/connections/connection_spec.rb +27 -27
- data/test/connections/databases.ci.yml +16 -16
- data/test/connections/databases.example.yml +40 -40
- data/test/connections/databases.yml +40 -40
- data/test/db_test.rb +52 -52
- data/test/fixtures/article.rb +6 -6
- data/test/fixtures/articles.yml +6 -6
- data/test/fixtures/capitol.rb +3 -3
- data/test/fixtures/capitols.yml +16 -16
- data/test/fixtures/comment.rb +7 -7
- data/test/fixtures/comments.yml +15 -15
- data/test/fixtures/db_definitions/db2-create-tables.sql +126 -126
- data/test/fixtures/db_definitions/db2-drop-tables.sql +18 -18
- data/test/fixtures/db_definitions/mysql.sql +226 -226
- data/test/fixtures/db_definitions/oracle.drop.sql +49 -49
- data/test/fixtures/db_definitions/oracle.sql +246 -246
- data/test/fixtures/db_definitions/postgresql.sql +229 -229
- data/test/fixtures/db_definitions/sqlite.sql +214 -214
- data/test/fixtures/db_definitions/sqlserver.sql +211 -211
- data/test/fixtures/department.rb +5 -5
- data/test/fixtures/departments.yml +15 -15
- data/test/fixtures/dorm.rb +2 -2
- data/test/fixtures/dorms.yml +4 -4
- data/test/fixtures/employee.rb +10 -10
- data/test/fixtures/employees.yml +18 -18
- data/test/fixtures/group.rb +2 -2
- data/test/fixtures/groups.yml +6 -6
- data/test/fixtures/hack.rb +4 -4
- data/test/fixtures/hacks.yml +2 -2
- data/test/fixtures/membership.rb +6 -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/pk_called_id.rb +5 -5
- data/test/fixtures/pk_called_ids.yml +11 -11
- 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_code_using_composite_key_alias.rb +8 -8
- data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
- 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/salary.rb +5 -5
- data/test/fixtures/seat.rb +5 -5
- data/test/fixtures/seats.yml +8 -8
- 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.rb +5 -5
- data/test/fixtures/topic_source.rb +6 -6
- data/test/fixtures/topic_sources.yml +3 -3
- data/test/fixtures/topics.yml +8 -8
- data/test/fixtures/user.rb +10 -10
- data/test/fixtures/users.yml +10 -10
- data/test/plugins/pagination.rb +405 -405
- data/test/plugins/pagination_helper.rb +135 -135
- data/test/setup.rb +50 -50
- data/test/test_aliases.rb +18 -18
- data/test/test_associations.rb +345 -345
- data/test/test_attribute_methods.rb +63 -63
- data/test/test_attributes.rb +60 -60
- data/test/test_calculations.rb +42 -42
- data/test/test_callbacks.rb +99 -99
- data/test/test_composite_arrays.rb +24 -24
- data/test/test_counter_cache.rb +30 -30
- data/test/test_create.rb +164 -164
- data/test/test_delete.rb +167 -167
- data/test/test_dumpable.rb +15 -15
- data/test/test_dup.rb +37 -37
- data/test/test_enum.rb +21 -21
- data/test/test_equal.rb +26 -26
- data/test/test_exists.rb +39 -39
- data/test/test_find.rb +137 -137
- data/test/test_habtm.rb +141 -141
- data/test/test_ids.rb +113 -113
- data/test/test_miscellaneous.rb +32 -32
- data/test/test_nested_attributes.rb +124 -124
- data/test/test_optimistic.rb +18 -18
- data/test/test_pagination.rb +35 -35
- data/test/test_polymorphic.rb +27 -27
- data/test/test_predicates.rb +59 -59
- data/test/test_preload.rb +94 -94
- data/test/test_santiago.rb +23 -23
- data/test/test_suite.rb +34 -34
- data/test/test_touch.rb +23 -23
- data/test/test_tutorial_example.rb +25 -25
- data/test/test_update.rb +76 -76
- data/test/test_validations.rb +13 -13
- metadata +32 -33
- data/lib/composite_primary_keys/locking/optimistic.rb +0 -56
@@ -1,229 +1,229 @@
|
|
1
|
-
module CompositePrimaryKeys
|
2
|
-
module ActiveRecord
|
3
|
-
module FinderMethods
|
4
|
-
def apply_join_dependency(join_dependency = construct_join_dependency)
|
5
|
-
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
|
6
|
-
|
7
|
-
if using_limitable_reflections?(join_dependency.reflections)
|
8
|
-
relation
|
9
|
-
else
|
10
|
-
if relation.limit_value
|
11
|
-
limited_ids = limited_ids_for(relation)
|
12
|
-
# CPK
|
13
|
-
# limited_ids.empty? ? relation.none! : relation.where!(primary_key => limited_ids)
|
14
|
-
limited_ids.empty? ? relation.none! : relation.where!(cpk_in_predicate(table, self.primary_keys, limited_ids))
|
15
|
-
end
|
16
|
-
relation.except(:limit, :offset)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def limited_ids_for(relation)
|
21
|
-
# CPK
|
22
|
-
# values = @klass.connection.columns_for_distinct(
|
23
|
-
# connection.column_name_from_arel_node(arel_attribute(primary_key)),
|
24
|
-
# relation.order_values
|
25
|
-
# )
|
26
|
-
|
27
|
-
columns = @klass.primary_keys.map do |key|
|
28
|
-
connection.column_name_from_arel_node(arel_attribute(key))
|
29
|
-
end
|
30
|
-
values = @klass.connection.columns_for_distinct(columns, relation.order_values)
|
31
|
-
|
32
|
-
relation = relation.except(:select).select(values).distinct!
|
33
|
-
|
34
|
-
id_rows = skip_query_cache_if_necessary { @klass.connection.select_all(relation.arel, "SQL") }
|
35
|
-
# CPK
|
36
|
-
#id_rows.map { |row| row[primary_key] }
|
37
|
-
id_rows.map do |row|
|
38
|
-
@klass.primary_keys.map do |key|
|
39
|
-
row[key]
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def construct_relation_for_exists(conditions)
|
45
|
-
relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
|
46
|
-
|
47
|
-
case conditions
|
48
|
-
# CPK
|
49
|
-
when CompositePrimaryKeys::CompositeKeys
|
50
|
-
relation = relation.where(cpk_id_predicate(table, primary_key, conditions))
|
51
|
-
# CPK
|
52
|
-
when Array
|
53
|
-
pk_length = @klass.primary_keys.length
|
54
|
-
|
55
|
-
if conditions.length == pk_length # E.g. conditions = ['France', 'Paris']
|
56
|
-
return self.construct_relation_for_exists(conditions.to_composite_keys)
|
57
|
-
else # Assume that conditions contains where relation
|
58
|
-
relation = relation.where(conditions)
|
59
|
-
end
|
60
|
-
when Array, Hash
|
61
|
-
relation.where!(conditions)
|
62
|
-
else
|
63
|
-
relation.where!(primary_key => conditions) unless conditions == :none
|
64
|
-
end
|
65
|
-
|
66
|
-
relation
|
67
|
-
end
|
68
|
-
|
69
|
-
def find_with_ids(*ids)
|
70
|
-
raise UnknownPrimaryKey.new(@klass) if primary_key.nil?
|
71
|
-
|
72
|
-
# CPK
|
73
|
-
# expects_array = ids.first.kind_of?(Array)
|
74
|
-
ids = CompositePrimaryKeys.normalize(ids)
|
75
|
-
expects_array = ids.flatten != ids.flatten(1)
|
76
|
-
return ids.first if expects_array && ids.first.empty?
|
77
|
-
|
78
|
-
# CPK
|
79
|
-
# ids = ids.flatten.compact.uniq
|
80
|
-
ids = expects_array ? ids.first : ids
|
81
|
-
|
82
|
-
model_name = @klass.name
|
83
|
-
|
84
|
-
case ids.size
|
85
|
-
when 0
|
86
|
-
error_message = "Couldn't find #{model_name} without an ID"
|
87
|
-
raise RecordNotFound.new(error_message, model_name, primary_key)
|
88
|
-
when 1
|
89
|
-
result = find_one(ids.first)
|
90
|
-
expects_array ? [ result ] : result
|
91
|
-
else
|
92
|
-
find_some(ids)
|
93
|
-
end
|
94
|
-
rescue ::RangeError
|
95
|
-
error_message = "Couldn't find #{model_name} with an out of range ID"
|
96
|
-
raise RecordNotFound.new(error_message, model_name, primary_key, ids)
|
97
|
-
end
|
98
|
-
|
99
|
-
def last(limit = nil)
|
100
|
-
return find_last(limit) if loaded? || limit_value
|
101
|
-
|
102
|
-
result = limit(limit || 1)
|
103
|
-
# CPK
|
104
|
-
# result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
|
105
|
-
if order_values.empty? && primary_key
|
106
|
-
if composite?
|
107
|
-
result.order!(primary_keys.map { |pk| arel_attribute(pk).asc })
|
108
|
-
elsif
|
109
|
-
result.order!(arel_attribute(primary_key))
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
result = result.reverse_order!
|
114
|
-
|
115
|
-
limit ? result.reverse : result.first
|
116
|
-
rescue ::ActiveRecord::IrreversibleOrderError
|
117
|
-
ActiveSupport::Deprecation.warn(<<-WARNING.squish)
|
118
|
-
Finding a last element by loading the relation when SQL ORDER
|
119
|
-
can not be reversed is deprecated.
|
120
|
-
Rails 5.1 will raise ActiveRecord::IrreversibleOrderError in this case.
|
121
|
-
Please call `to_a.last` if you still want to load the relation.
|
122
|
-
WARNING
|
123
|
-
find_last(limit)
|
124
|
-
end
|
125
|
-
|
126
|
-
def find_one(id)
|
127
|
-
# CPK
|
128
|
-
# if ActiveRecord::Base === id
|
129
|
-
if ::ActiveRecord::Base === id
|
130
|
-
id = id.id
|
131
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
132
|
-
You are passing an instance of ActiveRecord::Base to `find`.
|
133
|
-
Please pass the id of the object by calling `.id`
|
134
|
-
MSG
|
135
|
-
end
|
136
|
-
|
137
|
-
# CPK
|
138
|
-
#relation = where(primary_key => id)
|
139
|
-
relation = where(cpk_id_predicate(table, primary_keys, id))
|
140
|
-
record = relation.take
|
141
|
-
|
142
|
-
raise_record_not_found_exception!(id, 0, 1) unless record
|
143
|
-
|
144
|
-
record
|
145
|
-
end
|
146
|
-
|
147
|
-
def find_some(ids)
|
148
|
-
# CPK
|
149
|
-
if composite?
|
150
|
-
ids = if ids.length == 1
|
151
|
-
ids.first.split(CompositePrimaryKeys::ID_SEP).to_composite_keys
|
152
|
-
else
|
153
|
-
ids.to_composite_keys
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
return find_some_ordered(ids) unless order_values.present?
|
158
|
-
|
159
|
-
# CPK
|
160
|
-
# result = where(primary_key => ids).to_a
|
161
|
-
result = if composite?
|
162
|
-
result = where(cpk_in_predicate(table, primary_keys, ids)).to_a
|
163
|
-
else
|
164
|
-
result = where(primary_key => ids).to_a
|
165
|
-
end
|
166
|
-
|
167
|
-
expected_size =
|
168
|
-
if limit_value && ids.size > limit_value
|
169
|
-
limit_value
|
170
|
-
else
|
171
|
-
ids.size
|
172
|
-
end
|
173
|
-
|
174
|
-
# 11 ids with limit 3, offset 9 should give 2 results.
|
175
|
-
if offset_value && (ids.size - offset_value < expected_size)
|
176
|
-
expected_size = ids.size - offset_value
|
177
|
-
end
|
178
|
-
|
179
|
-
if result.size == expected_size
|
180
|
-
result
|
181
|
-
else
|
182
|
-
raise_record_not_found_exception!(ids, result.size, expected_size)
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def find_some_ordered(ids)
|
187
|
-
ids = ids.slice(offset_value || 0, limit_value || ids.size) || []
|
188
|
-
|
189
|
-
# CPK
|
190
|
-
# result = except(:limit, :offset).where(primary_key => ids).records
|
191
|
-
result = if composite?
|
192
|
-
except(:limit, :offset).where(cpk_in_predicate(table, primary_keys, ids)).records
|
193
|
-
else
|
194
|
-
except(:limit, :offset).where(primary_key => ids).records
|
195
|
-
end
|
196
|
-
|
197
|
-
if result.size == ids.size
|
198
|
-
pk_type = @klass.type_for_attribute(primary_key)
|
199
|
-
|
200
|
-
records_by_id = result.index_by(&:id)
|
201
|
-
# CPK
|
202
|
-
# ids.map { |id| records_by_id.fetch(pk_type.cast(id)) }
|
203
|
-
if composite?
|
204
|
-
ids.map do |id|
|
205
|
-
typecasted_id = primary_keys.zip(id).map do |col, val|
|
206
|
-
@klass.type_for_attribute(col).cast(val)
|
207
|
-
end
|
208
|
-
records_by_id.fetch(typecasted_id)
|
209
|
-
end
|
210
|
-
else
|
211
|
-
ids.map { |id| records_by_id.fetch(pk_type.cast(id)) }
|
212
|
-
end
|
213
|
-
else
|
214
|
-
raise_record_not_found_exception!(ids, result.size, ids.size)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
def ordered_relation
|
219
|
-
if order_values.empty? && primary_key
|
220
|
-
# CPK
|
221
|
-
#order(arel_attribute(primary_key).asc)
|
222
|
-
order(Array(primary_key).map {|key| arel_attribute(key).asc})
|
223
|
-
else
|
224
|
-
self
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
module ActiveRecord
|
3
|
+
module FinderMethods
|
4
|
+
def apply_join_dependency(join_dependency = construct_join_dependency)
|
5
|
+
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
|
6
|
+
|
7
|
+
if using_limitable_reflections?(join_dependency.reflections)
|
8
|
+
relation
|
9
|
+
else
|
10
|
+
if relation.limit_value
|
11
|
+
limited_ids = limited_ids_for(relation)
|
12
|
+
# CPK
|
13
|
+
# limited_ids.empty? ? relation.none! : relation.where!(primary_key => limited_ids)
|
14
|
+
limited_ids.empty? ? relation.none! : relation.where!(cpk_in_predicate(table, self.primary_keys, limited_ids))
|
15
|
+
end
|
16
|
+
relation.except(:limit, :offset)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def limited_ids_for(relation)
|
21
|
+
# CPK
|
22
|
+
# values = @klass.connection.columns_for_distinct(
|
23
|
+
# connection.column_name_from_arel_node(arel_attribute(primary_key)),
|
24
|
+
# relation.order_values
|
25
|
+
# )
|
26
|
+
|
27
|
+
columns = @klass.primary_keys.map do |key|
|
28
|
+
connection.column_name_from_arel_node(arel_attribute(key))
|
29
|
+
end
|
30
|
+
values = @klass.connection.columns_for_distinct(columns, relation.order_values)
|
31
|
+
|
32
|
+
relation = relation.except(:select).select(values).distinct!
|
33
|
+
|
34
|
+
id_rows = skip_query_cache_if_necessary { @klass.connection.select_all(relation.arel, "SQL") }
|
35
|
+
# CPK
|
36
|
+
#id_rows.map { |row| row[primary_key] }
|
37
|
+
id_rows.map do |row|
|
38
|
+
@klass.primary_keys.map do |key|
|
39
|
+
row[key]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def construct_relation_for_exists(conditions)
|
45
|
+
relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
|
46
|
+
|
47
|
+
case conditions
|
48
|
+
# CPK
|
49
|
+
when CompositePrimaryKeys::CompositeKeys
|
50
|
+
relation = relation.where(cpk_id_predicate(table, primary_key, conditions))
|
51
|
+
# CPK
|
52
|
+
when Array
|
53
|
+
pk_length = @klass.primary_keys.length
|
54
|
+
|
55
|
+
if conditions.length == pk_length # E.g. conditions = ['France', 'Paris']
|
56
|
+
return self.construct_relation_for_exists(conditions.to_composite_keys)
|
57
|
+
else # Assume that conditions contains where relation
|
58
|
+
relation = relation.where(conditions)
|
59
|
+
end
|
60
|
+
when Array, Hash
|
61
|
+
relation.where!(conditions)
|
62
|
+
else
|
63
|
+
relation.where!(primary_key => conditions) unless conditions == :none
|
64
|
+
end
|
65
|
+
|
66
|
+
relation
|
67
|
+
end
|
68
|
+
|
69
|
+
def find_with_ids(*ids)
|
70
|
+
raise UnknownPrimaryKey.new(@klass) if primary_key.nil?
|
71
|
+
|
72
|
+
# CPK
|
73
|
+
# expects_array = ids.first.kind_of?(Array)
|
74
|
+
ids = CompositePrimaryKeys.normalize(ids)
|
75
|
+
expects_array = ids.flatten != ids.flatten(1)
|
76
|
+
return ids.first if expects_array && ids.first.empty?
|
77
|
+
|
78
|
+
# CPK
|
79
|
+
# ids = ids.flatten.compact.uniq
|
80
|
+
ids = expects_array ? ids.first : ids
|
81
|
+
|
82
|
+
model_name = @klass.name
|
83
|
+
|
84
|
+
case ids.size
|
85
|
+
when 0
|
86
|
+
error_message = "Couldn't find #{model_name} without an ID"
|
87
|
+
raise RecordNotFound.new(error_message, model_name, primary_key)
|
88
|
+
when 1
|
89
|
+
result = find_one(ids.first)
|
90
|
+
expects_array ? [ result ] : result
|
91
|
+
else
|
92
|
+
find_some(ids)
|
93
|
+
end
|
94
|
+
rescue ::RangeError
|
95
|
+
error_message = "Couldn't find #{model_name} with an out of range ID"
|
96
|
+
raise RecordNotFound.new(error_message, model_name, primary_key, ids)
|
97
|
+
end
|
98
|
+
|
99
|
+
def last(limit = nil)
|
100
|
+
return find_last(limit) if loaded? || limit_value
|
101
|
+
|
102
|
+
result = limit(limit || 1)
|
103
|
+
# CPK
|
104
|
+
# result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
|
105
|
+
if order_values.empty? && primary_key
|
106
|
+
if composite?
|
107
|
+
result.order!(primary_keys.map { |pk| arel_attribute(pk).asc })
|
108
|
+
elsif
|
109
|
+
result.order!(arel_attribute(primary_key))
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
result = result.reverse_order!
|
114
|
+
|
115
|
+
limit ? result.reverse : result.first
|
116
|
+
rescue ::ActiveRecord::IrreversibleOrderError
|
117
|
+
ActiveSupport::Deprecation.warn(<<-WARNING.squish)
|
118
|
+
Finding a last element by loading the relation when SQL ORDER
|
119
|
+
can not be reversed is deprecated.
|
120
|
+
Rails 5.1 will raise ActiveRecord::IrreversibleOrderError in this case.
|
121
|
+
Please call `to_a.last` if you still want to load the relation.
|
122
|
+
WARNING
|
123
|
+
find_last(limit)
|
124
|
+
end
|
125
|
+
|
126
|
+
def find_one(id)
|
127
|
+
# CPK
|
128
|
+
# if ActiveRecord::Base === id
|
129
|
+
if ::ActiveRecord::Base === id
|
130
|
+
id = id.id
|
131
|
+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
132
|
+
You are passing an instance of ActiveRecord::Base to `find`.
|
133
|
+
Please pass the id of the object by calling `.id`
|
134
|
+
MSG
|
135
|
+
end
|
136
|
+
|
137
|
+
# CPK
|
138
|
+
#relation = where(primary_key => id)
|
139
|
+
relation = where(cpk_id_predicate(table, primary_keys, id))
|
140
|
+
record = relation.take
|
141
|
+
|
142
|
+
raise_record_not_found_exception!(id, 0, 1) unless record
|
143
|
+
|
144
|
+
record
|
145
|
+
end
|
146
|
+
|
147
|
+
def find_some(ids)
|
148
|
+
# CPK
|
149
|
+
if composite?
|
150
|
+
ids = if ids.length == 1
|
151
|
+
ids.first.split(CompositePrimaryKeys::ID_SEP).to_composite_keys
|
152
|
+
else
|
153
|
+
ids.to_composite_keys
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
return find_some_ordered(ids) unless order_values.present?
|
158
|
+
|
159
|
+
# CPK
|
160
|
+
# result = where(primary_key => ids).to_a
|
161
|
+
result = if composite?
|
162
|
+
result = where(cpk_in_predicate(table, primary_keys, ids)).to_a
|
163
|
+
else
|
164
|
+
result = where(primary_key => ids).to_a
|
165
|
+
end
|
166
|
+
|
167
|
+
expected_size =
|
168
|
+
if limit_value && ids.size > limit_value
|
169
|
+
limit_value
|
170
|
+
else
|
171
|
+
ids.size
|
172
|
+
end
|
173
|
+
|
174
|
+
# 11 ids with limit 3, offset 9 should give 2 results.
|
175
|
+
if offset_value && (ids.size - offset_value < expected_size)
|
176
|
+
expected_size = ids.size - offset_value
|
177
|
+
end
|
178
|
+
|
179
|
+
if result.size == expected_size
|
180
|
+
result
|
181
|
+
else
|
182
|
+
raise_record_not_found_exception!(ids, result.size, expected_size)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def find_some_ordered(ids)
|
187
|
+
ids = ids.slice(offset_value || 0, limit_value || ids.size) || []
|
188
|
+
|
189
|
+
# CPK
|
190
|
+
# result = except(:limit, :offset).where(primary_key => ids).records
|
191
|
+
result = if composite?
|
192
|
+
except(:limit, :offset).where(cpk_in_predicate(table, primary_keys, ids)).records
|
193
|
+
else
|
194
|
+
except(:limit, :offset).where(primary_key => ids).records
|
195
|
+
end
|
196
|
+
|
197
|
+
if result.size == ids.size
|
198
|
+
pk_type = @klass.type_for_attribute(primary_key)
|
199
|
+
|
200
|
+
records_by_id = result.index_by(&:id)
|
201
|
+
# CPK
|
202
|
+
# ids.map { |id| records_by_id.fetch(pk_type.cast(id)) }
|
203
|
+
if composite?
|
204
|
+
ids.map do |id|
|
205
|
+
typecasted_id = primary_keys.zip(id).map do |col, val|
|
206
|
+
@klass.type_for_attribute(col).cast(val)
|
207
|
+
end
|
208
|
+
records_by_id.fetch(typecasted_id)
|
209
|
+
end
|
210
|
+
else
|
211
|
+
ids.map { |id| records_by_id.fetch(pk_type.cast(id)) }
|
212
|
+
end
|
213
|
+
else
|
214
|
+
raise_record_not_found_exception!(ids, result.size, ids.size)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def ordered_relation
|
219
|
+
if order_values.empty? && primary_key
|
220
|
+
# CPK
|
221
|
+
#order(arel_attribute(primary_key).asc)
|
222
|
+
order(Array(primary_key).map {|key| arel_attribute(key).asc})
|
223
|
+
else
|
224
|
+
self
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|