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,44 +1,44 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module ConnectionAdapters
|
3
|
-
module SQLServer
|
4
|
-
module DatabaseStatements
|
5
|
-
def sql_for_insert(sql, pk, binds)
|
6
|
-
if pk.nil?
|
7
|
-
table_name = query_requires_identity_insert?(sql)
|
8
|
-
pk = primary_key(table_name)
|
9
|
-
end
|
10
|
-
|
11
|
-
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
|
12
|
-
# CPK
|
13
|
-
#quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
|
14
|
-
quoted_pk = Array(pk).map {|subkey| SQLServer::Utils.extract_identifiers(subkey).quoted}
|
15
|
-
|
16
|
-
table_name ||= get_table_name(sql)
|
17
|
-
exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
|
18
|
-
if exclude_output_inserted
|
19
|
-
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
|
20
|
-
# CPK
|
21
|
-
# <<~SQL.squish
|
22
|
-
# DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
|
23
|
-
# #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
|
24
|
-
# SELECT CAST(#{quoted_pk.join(',')} AS #{id_sql_type}) FROM @ssaIdInsertTable
|
25
|
-
# SQL
|
26
|
-
<<~SQL.squish
|
27
|
-
DECLARE @ssaIdInsertTable table (#{quoted_pk.map {|subkey| "#{subkey} #{id_sql_type}"}.join(", ")});
|
28
|
-
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')} INTO @ssaIdInsertTable"}
|
29
|
-
SELECT #{quoted_pk.map {|subkey| "CAST(#{subkey} AS #{id_sql_type}) #{subkey}"}.join(", ")} FROM @ssaIdInsertTable
|
30
|
-
SQL
|
31
|
-
else
|
32
|
-
# CPK
|
33
|
-
# sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
|
34
|
-
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')}"
|
35
|
-
end
|
36
|
-
else
|
37
|
-
"#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
|
38
|
-
end
|
39
|
-
super
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module SQLServer
|
4
|
+
module DatabaseStatements
|
5
|
+
def sql_for_insert(sql, pk, binds)
|
6
|
+
if pk.nil?
|
7
|
+
table_name = query_requires_identity_insert?(sql)
|
8
|
+
pk = primary_key(table_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
|
12
|
+
# CPK
|
13
|
+
#quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
|
14
|
+
quoted_pk = Array(pk).map {|subkey| SQLServer::Utils.extract_identifiers(subkey).quoted}
|
15
|
+
|
16
|
+
table_name ||= get_table_name(sql)
|
17
|
+
exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
|
18
|
+
if exclude_output_inserted
|
19
|
+
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
|
20
|
+
# CPK
|
21
|
+
# <<~SQL.squish
|
22
|
+
# DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
|
23
|
+
# #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
|
24
|
+
# SELECT CAST(#{quoted_pk.join(',')} AS #{id_sql_type}) FROM @ssaIdInsertTable
|
25
|
+
# SQL
|
26
|
+
<<~SQL.squish
|
27
|
+
DECLARE @ssaIdInsertTable table (#{quoted_pk.map {|subkey| "#{subkey} #{id_sql_type}"}.join(", ")});
|
28
|
+
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')} INTO @ssaIdInsertTable"}
|
29
|
+
SELECT #{quoted_pk.map {|subkey| "CAST(#{subkey} AS #{id_sql_type}) #{subkey}"}.join(", ")} FROM @ssaIdInsertTable
|
30
|
+
SQL
|
31
|
+
else
|
32
|
+
# CPK
|
33
|
+
# sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
|
34
|
+
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk.join(', INSERTED.')}"
|
35
|
+
end
|
36
|
+
else
|
37
|
+
"#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
|
38
|
+
end
|
39
|
+
super
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,49 +1,49 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Core
|
3
|
-
def initialize_dup(other) # :nodoc:
|
4
|
-
@attributes = @attributes.deep_dup
|
5
|
-
# CPK
|
6
|
-
#@attributes.reset(@primary_key)
|
7
|
-
Array(self.class.primary_key).each {|key| @attributes.reset(key)}
|
8
|
-
|
9
|
-
_run_initialize_callbacks
|
10
|
-
|
11
|
-
@new_record = true
|
12
|
-
@destroyed = false
|
13
|
-
@_start_transaction_state = nil
|
14
|
-
@transaction_state = nil
|
15
|
-
|
16
|
-
super
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
def find(*ids) # :nodoc:
|
21
|
-
# We don't have cache keys for this stuff yet
|
22
|
-
return super unless ids.length == 1
|
23
|
-
return super if block_given? ||
|
24
|
-
primary_key.nil? ||
|
25
|
-
scope_attributes? ||
|
26
|
-
columns_hash.key?(inheritance_column) && !base_class?
|
27
|
-
|
28
|
-
# CPK
|
29
|
-
return super if self.composite?
|
30
|
-
|
31
|
-
id = ids.first
|
32
|
-
|
33
|
-
return super if StatementCache.unsupported_value?(id)
|
34
|
-
|
35
|
-
key = primary_key
|
36
|
-
|
37
|
-
statement = cached_find_by_statement(key) { |params|
|
38
|
-
where(key => params.bind).limit(1)
|
39
|
-
}
|
40
|
-
|
41
|
-
record = statement.execute([id], connection)&.first
|
42
|
-
unless record
|
43
|
-
raise ::ActiveRecord::RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)
|
44
|
-
end
|
45
|
-
record
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Core
|
3
|
+
def initialize_dup(other) # :nodoc:
|
4
|
+
@attributes = @attributes.deep_dup
|
5
|
+
# CPK
|
6
|
+
#@attributes.reset(@primary_key)
|
7
|
+
Array(self.class.primary_key).each {|key| @attributes.reset(key)}
|
8
|
+
|
9
|
+
_run_initialize_callbacks
|
10
|
+
|
11
|
+
@new_record = true
|
12
|
+
@destroyed = false
|
13
|
+
@_start_transaction_state = nil
|
14
|
+
@transaction_state = nil
|
15
|
+
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def find(*ids) # :nodoc:
|
21
|
+
# We don't have cache keys for this stuff yet
|
22
|
+
return super unless ids.length == 1
|
23
|
+
return super if block_given? ||
|
24
|
+
primary_key.nil? ||
|
25
|
+
scope_attributes? ||
|
26
|
+
columns_hash.key?(inheritance_column) && !base_class?
|
27
|
+
|
28
|
+
# CPK
|
29
|
+
return super if self.composite?
|
30
|
+
|
31
|
+
id = ids.first
|
32
|
+
|
33
|
+
return super if StatementCache.unsupported_value?(id)
|
34
|
+
|
35
|
+
key = primary_key
|
36
|
+
|
37
|
+
statement = cached_find_by_statement(key) { |params|
|
38
|
+
where(key => params.bind).limit(1)
|
39
|
+
}
|
40
|
+
|
41
|
+
record = statement.execute([id], connection)&.first
|
42
|
+
unless record
|
43
|
+
raise ::ActiveRecord::RecordNotFound.new("Couldn't find #{name} with '#{key}'=#{id}", name, key, id)
|
44
|
+
end
|
45
|
+
record
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
49
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module CounterCache
|
3
|
-
module ClassMethods
|
4
|
-
def update_counters(id, counters)
|
5
|
-
# CPK
|
6
|
-
if self.composite?
|
7
|
-
predicate = cpk_id_predicate(self.arel_table, primary_key, id)
|
8
|
-
unscoped.where!(predicate).update_counters(counters)
|
9
|
-
else
|
10
|
-
unscoped.where!(primary_key => id).update_counters(counters)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module CounterCache
|
3
|
+
module ClassMethods
|
4
|
+
def update_counters(id, counters)
|
5
|
+
# CPK
|
6
|
+
if self.composite?
|
7
|
+
predicate = cpk_id_predicate(self.arel_table, primary_key, id)
|
8
|
+
unscoped.where!(predicate).update_counters(counters)
|
9
|
+
else
|
10
|
+
unscoped.where!(primary_key => id).update_counters(counters)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,21 +1,21 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
class Fixture
|
3
|
-
def find
|
4
|
-
raise FixtureClassNotFound, "No class attached to find." unless model_class
|
5
|
-
model_class.unscoped do
|
6
|
-
# CPK
|
7
|
-
#model_class.find(fixture[model_class.primary_key])
|
8
|
-
ids = self.ids(model_class.primary_key)
|
9
|
-
model_class.find(ids)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def ids(key)
|
14
|
-
if key.is_a? Array
|
15
|
-
key.map {|a_key| fixture[a_key.to_s] }
|
16
|
-
else
|
17
|
-
fixture[key]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
class Fixture
|
3
|
+
def find
|
4
|
+
raise FixtureClassNotFound, "No class attached to find." unless model_class
|
5
|
+
model_class.unscoped do
|
6
|
+
# CPK
|
7
|
+
#model_class.find(fixture[model_class.primary_key])
|
8
|
+
ids = self.ids(model_class.primary_key)
|
9
|
+
model_class.find(ids)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def ids(key)
|
14
|
+
if key.is_a? Array
|
15
|
+
key.map {|a_key| fixture[a_key.to_s] }
|
16
|
+
else
|
17
|
+
fixture[key]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -70,7 +70,7 @@ module ActiveRecord
|
|
70
70
|
if target_record
|
71
71
|
existing_record = target_record
|
72
72
|
else
|
73
|
-
association.add_to_target(existing_record, :
|
73
|
+
association.add_to_target(existing_record, skip_callbacks: true)
|
74
74
|
end
|
75
75
|
|
76
76
|
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
|
@@ -10,11 +10,12 @@ module ActiveRecord
|
|
10
10
|
Array(id_or_array)
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
# Delete should return the number of deleted records
|
14
|
+
id_or_array.map do |id|
|
14
15
|
# Is the passed in id actually a record?
|
15
16
|
id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
|
16
17
|
delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
|
17
|
-
end
|
18
|
+
end.sum
|
18
19
|
else
|
19
20
|
delete_by(primary_key => id_or_array)
|
20
21
|
end
|
@@ -1,29 +1,91 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Reflection
|
3
|
-
class AbstractReflection
|
4
|
-
def join_scope(table, foreign_table, foreign_klass)
|
5
|
-
predicate_builder = predicate_builder(table)
|
6
|
-
scope_chain_items = join_scopes(table, predicate_builder)
|
7
|
-
klass_scope = klass_join_scope(table, predicate_builder)
|
8
|
-
|
9
|
-
key =
|
10
|
-
foreign_key =
|
11
|
-
|
12
|
-
# CPK
|
13
|
-
#klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
|
14
|
-
constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
|
15
|
-
klass_scope.where!(constraint)
|
16
|
-
|
17
|
-
if type
|
18
|
-
klass_scope.where!(type => foreign_klass.polymorphic_name)
|
19
|
-
end
|
20
|
-
|
21
|
-
if klass.finder_needs_type_condition?
|
22
|
-
klass_scope.where!(klass.send(:type_condition, table))
|
23
|
-
end
|
24
|
-
|
25
|
-
scope_chain_items.inject(klass_scope, &:merge!)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
|
1
|
+
module ActiveRecord
|
2
|
+
module Reflection
|
3
|
+
class AbstractReflection
|
4
|
+
def join_scope(table, foreign_table, foreign_klass)
|
5
|
+
predicate_builder = predicate_builder(table)
|
6
|
+
scope_chain_items = join_scopes(table, predicate_builder)
|
7
|
+
klass_scope = klass_join_scope(table, predicate_builder)
|
8
|
+
|
9
|
+
key = join_primary_key
|
10
|
+
foreign_key = join_foreign_key
|
11
|
+
|
12
|
+
# CPK
|
13
|
+
#klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
|
14
|
+
constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
|
15
|
+
klass_scope.where!(constraint)
|
16
|
+
|
17
|
+
if type
|
18
|
+
klass_scope.where!(type => foreign_klass.polymorphic_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
if klass.finder_needs_type_condition?
|
22
|
+
klass_scope.where!(klass.send(:type_condition, table))
|
23
|
+
end
|
24
|
+
|
25
|
+
scope_chain_items.inject(klass_scope, &:merge!)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class AssociationReflection < MacroReflection
|
30
|
+
def foreign_key
|
31
|
+
# CPK
|
32
|
+
# @foreign_key ||= -(options[:foreign_key]&.to_s || derive_foreign_key)
|
33
|
+
@foreign_key ||= extract_keys(options[:foreign_key]) || derive_foreign_key
|
34
|
+
end
|
35
|
+
|
36
|
+
def association_foreign_key
|
37
|
+
# CPK
|
38
|
+
# @association_foreign_key ||= -(options[:association_foreign_key]&.to_s || class_name.foreign_key)
|
39
|
+
@association_foreign_key ||= extract_keys(options[:association_foreign_key]) || class_name.foreign_key
|
40
|
+
end
|
41
|
+
|
42
|
+
def active_record_primary_key
|
43
|
+
# CPK (Rails freezes the string returned in the expression that calculates PK here. But Rails uses the `-` method which is not available on Array for CPK, so we calculate it in one line and freeze it on the next)
|
44
|
+
# @active_record_primary_key ||= -(options[:primary_key]&.to_s || primary_key(active_record))
|
45
|
+
@active_record_primary_key ||= begin
|
46
|
+
pk = options[:primary_key] || primary_key(active_record)
|
47
|
+
pk.freeze
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def extract_keys(keys)
|
54
|
+
case keys
|
55
|
+
when Array
|
56
|
+
keys.map { |k| k.to_s }
|
57
|
+
when NilClass
|
58
|
+
nil
|
59
|
+
else
|
60
|
+
keys.to_s
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class BelongsToReflection < AssociationReflection
|
66
|
+
def association_primary_key(klass = nil)
|
67
|
+
if primary_key = options[:primary_key]
|
68
|
+
# CPK
|
69
|
+
# @association_primary_key ||= -primary_key.to_s
|
70
|
+
@association_primary_key ||= primary_key.freeze
|
71
|
+
else
|
72
|
+
primary_key(klass || self.klass)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class ThroughReflection < AbstractReflection #:nodoc:
|
78
|
+
def association_primary_key(klass = nil)
|
79
|
+
# Get the "actual" source reflection if the immediate source reflection has a
|
80
|
+
# source reflection itself
|
81
|
+
if primary_key = actual_source_reflection.options[:primary_key]
|
82
|
+
# CPK
|
83
|
+
# @association_primary_key ||= -primary_key.to_s
|
84
|
+
@association_primary_key ||= primary_key.freeze
|
85
|
+
else
|
86
|
+
primary_key(klass || self.klass)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
module CompositePrimaryKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module Batches
|
4
|
-
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
|
4
|
+
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil, order: :asc)
|
5
5
|
relation = self
|
6
6
|
unless block_given?
|
7
7
|
return ::ActiveRecord::Batches::BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
|
8
8
|
end
|
9
9
|
|
10
|
+
unless [:asc, :desc].include?(order)
|
11
|
+
raise ArgumentError, ":order must be :asc or :desc, got #{order.inspect}"
|
12
|
+
end
|
13
|
+
|
10
14
|
if arel.orders.present?
|
11
15
|
act_on_ignored_order(error_on_ignore)
|
12
16
|
end
|
@@ -17,8 +21,8 @@ module CompositePrimaryKeys
|
|
17
21
|
batch_limit = remaining if remaining < batch_limit
|
18
22
|
end
|
19
23
|
|
20
|
-
relation = relation.reorder(batch_order).limit(batch_limit)
|
21
|
-
relation = apply_limits(relation, start, finish)
|
24
|
+
relation = relation.reorder(batch_order(order)).limit(batch_limit)
|
25
|
+
relation = apply_limits(relation, start, finish, order)
|
22
26
|
relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
|
23
27
|
batch_relation = relation
|
24
28
|
|
@@ -61,7 +65,9 @@ module CompositePrimaryKeys
|
|
61
65
|
end
|
62
66
|
|
63
67
|
# CPK
|
64
|
-
#
|
68
|
+
#batch_relation = relation.where(
|
69
|
+
# predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
|
70
|
+
#)
|
65
71
|
batch_relation = if composite?
|
66
72
|
# CPK
|
67
73
|
# Lexicographically select records
|
@@ -81,7 +87,9 @@ module CompositePrimaryKeys
|
|
81
87
|
end.reduce(:or)
|
82
88
|
relation.where(query)
|
83
89
|
else
|
84
|
-
relation.where(
|
90
|
+
batch_relation = relation.where(
|
91
|
+
predicate_builder[primary_key, primary_key_offset, order == :desc ? :lt : :gt]
|
92
|
+
)
|
85
93
|
end
|
86
94
|
end
|
87
95
|
end
|
@@ -95,9 +103,9 @@ module CompositePrimaryKeys
|
|
95
103
|
ary.length.times.reduce([]) { |results, i| results << ary[0..i] }
|
96
104
|
end
|
97
105
|
|
98
|
-
def batch_order
|
106
|
+
def batch_order(order)
|
99
107
|
self.primary_key.map do |key|
|
100
|
-
|
108
|
+
table[key].public_send(order)
|
101
109
|
end
|
102
110
|
end
|
103
111
|
end
|
@@ -4,11 +4,12 @@ module CompositePrimaryKeys
|
|
4
4
|
def aggregate_column(column_name)
|
5
5
|
# CPK
|
6
6
|
if column_name.kind_of?(Array)
|
7
|
+
# Note: Test don't seem to run this code?
|
7
8
|
column_name.map do |column|
|
8
|
-
@klass.
|
9
|
+
@klass.arel_table[column]
|
9
10
|
end
|
10
11
|
elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
|
11
|
-
@klass.
|
12
|
+
@klass.arel_table[column_name]
|
12
13
|
else
|
13
14
|
Arel.sql(column_name == :all ? "*" : column_name.to_s)
|
14
15
|
end
|
@@ -31,38 +32,33 @@ module CompositePrimaryKeys
|
|
31
32
|
relation = unscope(:order).distinct!(false)
|
32
33
|
|
33
34
|
column = aggregate_column(column_name)
|
34
|
-
|
35
35
|
select_value = operation_over_aggregate_column(column, operation, distinct)
|
36
|
-
if operation == "sum" && distinct
|
37
|
-
select_value.distinct = true
|
38
|
-
end
|
36
|
+
select_value.distinct = true if operation == "sum" && distinct
|
39
37
|
|
40
|
-
column_alias = select_value.alias
|
41
|
-
column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
|
42
38
|
relation.select_values = [select_value]
|
43
39
|
|
44
40
|
query_builder = relation.arel
|
45
41
|
end
|
46
42
|
|
47
|
-
result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder
|
48
|
-
row = result.first
|
49
|
-
value = row && row.values.first
|
50
|
-
type = result.column_types.fetch(column_alias) do
|
51
|
-
type_for(column_name)
|
52
|
-
end
|
43
|
+
result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder) }
|
53
44
|
|
54
|
-
type_cast_calculated_value(
|
45
|
+
type_cast_calculated_value(result.cast_values.first, operation) do |value|
|
46
|
+
type = column.try(:type_caster) ||
|
47
|
+
# CPK
|
48
|
+
# lookup_cast_type_from_join_dependencies(column_name.to_s) || Type.default_value
|
49
|
+
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
50
|
+
type.deserialize(value)
|
51
|
+
end
|
55
52
|
end
|
56
53
|
|
57
54
|
def build_count_subquery(relation, column_name, distinct)
|
58
55
|
if column_name == :all
|
56
|
+
column_alias = Arel.star
|
57
|
+
# CPK
|
58
|
+
# relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
|
59
59
|
relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
|
60
|
-
if relation.select_values.first.is_a?(Array)
|
61
|
-
relation.select_values = relation.select_values.first.map do |column|
|
62
|
-
Arel::Attribute.new(@klass.unscoped.table, column)
|
63
|
-
end
|
64
|
-
end
|
65
60
|
elsif column_name.is_a?(Array)
|
61
|
+
column_alias = Arel.star
|
66
62
|
relation.select_values = column_name.map do |column|
|
67
63
|
Arel::Attribute.new(@klass.unscoped.table, column)
|
68
64
|
end
|
@@ -71,10 +67,37 @@ module CompositePrimaryKeys
|
|
71
67
|
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
|
72
68
|
end
|
73
69
|
|
74
|
-
|
75
|
-
select_value = operation_over_aggregate_column(column_alias
|
70
|
+
subquery_alias = Arel.sql("subquery_for_count")
|
71
|
+
select_value = operation_over_aggregate_column(column_alias, "count", false)
|
72
|
+
|
73
|
+
relation.build_subquery(subquery_alias, select_value)
|
74
|
+
end
|
75
|
+
|
76
|
+
def calculate(operation, column_name)
|
77
|
+
if has_include?(column_name)
|
78
|
+
relation = apply_join_dependency
|
76
79
|
|
77
|
-
|
80
|
+
if operation.to_s.downcase == "count"
|
81
|
+
unless distinct_value || distinct_select?(column_name || select_for_count)
|
82
|
+
relation.distinct!
|
83
|
+
# CPK
|
84
|
+
# relation.select_values = [ klass.primary_key || table[Arel.star] ]
|
85
|
+
if klass.primary_key.present? && klass.primary_key.is_a?(Array)
|
86
|
+
relation.select_values = klass.primary_key.map do |k|
|
87
|
+
"#{connection.quote_table_name(klass.table_name)}.#{connection.quote_column_name(k)}"
|
88
|
+
end
|
89
|
+
else
|
90
|
+
relation.select_values = [ klass.primary_key || table[Arel.star] ]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
|
94
|
+
relation.order_values = [] if group_values.empty?
|
95
|
+
end
|
96
|
+
|
97
|
+
relation.calculate(operation, column_name)
|
98
|
+
else
|
99
|
+
perform_calculation(operation, column_name)
|
100
|
+
end
|
78
101
|
end
|
79
102
|
end
|
80
103
|
end
|