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.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +774 -763
  3. data/Rakefile +37 -37
  4. data/lib/composite_primary_keys.rb +120 -122
  5. data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
  6. data/lib/composite_primary_keys/arel/to_sql.rb +32 -32
  7. data/lib/composite_primary_keys/associations/association.rb +23 -23
  8. data/lib/composite_primary_keys/associations/association_scope.rb +68 -68
  9. data/lib/composite_primary_keys/associations/collection_association.rb +31 -31
  10. data/lib/composite_primary_keys/associations/foreign_association.rb +15 -15
  11. data/lib/composite_primary_keys/associations/has_many_association.rb +34 -34
  12. data/lib/composite_primary_keys/associations/join_dependency.rb +102 -102
  13. data/lib/composite_primary_keys/associations/preloader/association.rb +117 -117
  14. data/lib/composite_primary_keys/attribute_methods.rb +9 -9
  15. data/lib/composite_primary_keys/attribute_methods/read.rb +27 -27
  16. data/lib/composite_primary_keys/attribute_methods/write.rb +44 -44
  17. data/lib/composite_primary_keys/autosave_association.rb +32 -32
  18. data/lib/composite_primary_keys/base.rb +130 -130
  19. data/lib/composite_primary_keys/composite_arrays.rb +51 -51
  20. data/lib/composite_primary_keys/composite_predicates.rb +69 -69
  21. data/lib/composite_primary_keys/composite_relation.rb +29 -29
  22. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  23. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +29 -29
  24. data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
  25. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +22 -22
  26. data/lib/composite_primary_keys/core.rb +54 -54
  27. data/lib/composite_primary_keys/counter_cache.rb +28 -28
  28. data/lib/composite_primary_keys/fixtures.rb +26 -26
  29. data/lib/composite_primary_keys/persistence.rb +120 -112
  30. data/lib/composite_primary_keys/reflection.rb +20 -20
  31. data/lib/composite_primary_keys/relation.rb +88 -88
  32. data/lib/composite_primary_keys/relation/calculations.rb +79 -79
  33. data/lib/composite_primary_keys/relation/finder_methods.rb +229 -229
  34. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +20 -20
  35. data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
  36. data/lib/composite_primary_keys/relation/where_clause.rb +24 -24
  37. data/lib/composite_primary_keys/sanitization.rb +42 -42
  38. data/lib/composite_primary_keys/validations/uniqueness.rb +32 -32
  39. data/lib/composite_primary_keys/version.rb +8 -8
  40. data/scripts/console.rb +48 -48
  41. data/scripts/txt2html +76 -76
  42. data/scripts/txt2js +65 -65
  43. data/tasks/databases/mysql.rake +40 -40
  44. data/tasks/databases/oracle.rake +41 -41
  45. data/tasks/databases/postgresql.rake +38 -38
  46. data/tasks/databases/sqlite.rake +25 -25
  47. data/tasks/databases/sqlserver.rake +43 -43
  48. data/tasks/website.rake +18 -18
  49. data/test/README_tests.rdoc +56 -56
  50. data/test/abstract_unit.rb +120 -120
  51. data/test/connections/connection_spec.rb +27 -27
  52. data/test/connections/databases.ci.yml +16 -16
  53. data/test/connections/databases.example.yml +40 -40
  54. data/test/connections/databases.yml +40 -40
  55. data/test/db_test.rb +52 -52
  56. data/test/fixtures/article.rb +6 -6
  57. data/test/fixtures/articles.yml +6 -6
  58. data/test/fixtures/capitol.rb +3 -3
  59. data/test/fixtures/capitols.yml +16 -16
  60. data/test/fixtures/comment.rb +7 -7
  61. data/test/fixtures/comments.yml +15 -15
  62. data/test/fixtures/db_definitions/db2-create-tables.sql +126 -126
  63. data/test/fixtures/db_definitions/db2-drop-tables.sql +18 -18
  64. data/test/fixtures/db_definitions/mysql.sql +226 -226
  65. data/test/fixtures/db_definitions/oracle.drop.sql +49 -49
  66. data/test/fixtures/db_definitions/oracle.sql +246 -246
  67. data/test/fixtures/db_definitions/postgresql.sql +229 -229
  68. data/test/fixtures/db_definitions/sqlite.sql +214 -214
  69. data/test/fixtures/db_definitions/sqlserver.sql +211 -211
  70. data/test/fixtures/department.rb +5 -5
  71. data/test/fixtures/departments.yml +15 -15
  72. data/test/fixtures/dorm.rb +2 -2
  73. data/test/fixtures/dorms.yml +4 -4
  74. data/test/fixtures/employee.rb +10 -10
  75. data/test/fixtures/employees.yml +18 -18
  76. data/test/fixtures/group.rb +2 -2
  77. data/test/fixtures/groups.yml +6 -6
  78. data/test/fixtures/hack.rb +4 -4
  79. data/test/fixtures/hacks.yml +2 -2
  80. data/test/fixtures/membership.rb +6 -6
  81. data/test/fixtures/membership_status.rb +2 -2
  82. data/test/fixtures/membership_statuses.yml +16 -16
  83. data/test/fixtures/memberships.yml +10 -10
  84. data/test/fixtures/pk_called_id.rb +5 -5
  85. data/test/fixtures/pk_called_ids.yml +11 -11
  86. data/test/fixtures/product.rb +9 -9
  87. data/test/fixtures/product_tariff.rb +5 -5
  88. data/test/fixtures/product_tariffs.yml +14 -14
  89. data/test/fixtures/products.yml +11 -11
  90. data/test/fixtures/reading.rb +4 -4
  91. data/test/fixtures/readings.yml +10 -10
  92. data/test/fixtures/reference_code.rb +7 -7
  93. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  94. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  95. data/test/fixtures/reference_codes.yml +28 -28
  96. data/test/fixtures/reference_type.rb +12 -12
  97. data/test/fixtures/reference_types.yml +9 -9
  98. data/test/fixtures/restaurant.rb +9 -9
  99. data/test/fixtures/restaurants.yml +14 -14
  100. data/test/fixtures/restaurants_suburb.rb +2 -2
  101. data/test/fixtures/restaurants_suburbs.yml +10 -10
  102. data/test/fixtures/room.rb +11 -11
  103. data/test/fixtures/room_assignment.rb +13 -13
  104. data/test/fixtures/room_assignments.yml +24 -24
  105. data/test/fixtures/room_attribute.rb +2 -2
  106. data/test/fixtures/room_attribute_assignment.rb +4 -4
  107. data/test/fixtures/room_attribute_assignments.yml +4 -4
  108. data/test/fixtures/room_attributes.yml +2 -2
  109. data/test/fixtures/rooms.yml +12 -12
  110. data/test/fixtures/salary.rb +5 -5
  111. data/test/fixtures/seat.rb +5 -5
  112. data/test/fixtures/seats.yml +8 -8
  113. data/test/fixtures/street.rb +2 -2
  114. data/test/fixtures/streets.yml +16 -16
  115. data/test/fixtures/student.rb +3 -3
  116. data/test/fixtures/students.yml +15 -15
  117. data/test/fixtures/suburb.rb +5 -5
  118. data/test/fixtures/suburbs.yml +14 -14
  119. data/test/fixtures/tariff.rb +5 -5
  120. data/test/fixtures/tariffs.yml +14 -14
  121. data/test/fixtures/topic.rb +5 -5
  122. data/test/fixtures/topic_source.rb +6 -6
  123. data/test/fixtures/topic_sources.yml +3 -3
  124. data/test/fixtures/topics.yml +8 -8
  125. data/test/fixtures/user.rb +10 -10
  126. data/test/fixtures/users.yml +10 -10
  127. data/test/plugins/pagination.rb +405 -405
  128. data/test/plugins/pagination_helper.rb +135 -135
  129. data/test/setup.rb +50 -50
  130. data/test/test_aliases.rb +18 -18
  131. data/test/test_associations.rb +345 -345
  132. data/test/test_attribute_methods.rb +63 -63
  133. data/test/test_attributes.rb +60 -60
  134. data/test/test_calculations.rb +42 -42
  135. data/test/test_callbacks.rb +99 -99
  136. data/test/test_composite_arrays.rb +24 -24
  137. data/test/test_counter_cache.rb +30 -30
  138. data/test/test_create.rb +164 -164
  139. data/test/test_delete.rb +167 -167
  140. data/test/test_dumpable.rb +15 -15
  141. data/test/test_dup.rb +37 -37
  142. data/test/test_enum.rb +21 -21
  143. data/test/test_equal.rb +26 -26
  144. data/test/test_exists.rb +39 -39
  145. data/test/test_find.rb +137 -137
  146. data/test/test_habtm.rb +141 -141
  147. data/test/test_ids.rb +113 -113
  148. data/test/test_miscellaneous.rb +32 -32
  149. data/test/test_nested_attributes.rb +124 -124
  150. data/test/test_optimistic.rb +18 -18
  151. data/test/test_pagination.rb +35 -35
  152. data/test/test_polymorphic.rb +27 -27
  153. data/test/test_predicates.rb +59 -59
  154. data/test/test_preload.rb +94 -94
  155. data/test/test_santiago.rb +23 -23
  156. data/test/test_suite.rb +34 -34
  157. data/test/test_touch.rb +23 -23
  158. data/test/test_tutorial_example.rb +25 -25
  159. data/test/test_update.rb +76 -76
  160. data/test/test_validations.rb +13 -13
  161. metadata +32 -33
  162. 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