composite_primary_keys 12.0.2 → 12.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +880 -841
- data/README.rdoc +180 -179
- data/lib/composite_primary_keys/active_model/attribute_assignment.rb +19 -0
- data/lib/composite_primary_keys/arel/sqlserver.rb +1 -3
- data/lib/composite_primary_keys/associations/association_scope.rb +68 -68
- data/lib/composite_primary_keys/associations/join_dependency.rb +103 -103
- data/lib/composite_primary_keys/associations/through_association.rb +2 -1
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -0
- 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 +9 -9
- data/lib/composite_primary_keys/base.rb +141 -130
- data/lib/composite_primary_keys/composite_arrays.rb +0 -8
- data/lib/composite_primary_keys/connection_adapters/abstract/database_statements.rb +37 -17
- data/lib/composite_primary_keys/connection_adapters/sqlserver/database_statements.rb +44 -23
- data/lib/composite_primary_keys/core.rb +48 -48
- data/lib/composite_primary_keys/persistence.rb +82 -81
- data/lib/composite_primary_keys/reflection.rb +29 -29
- data/lib/composite_primary_keys/relation/batches.rb +1 -1
- data/lib/composite_primary_keys/relation/calculations.rb +81 -81
- data/lib/composite_primary_keys/relation/finder_methods.rb +235 -235
- 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 +23 -23
- data/lib/composite_primary_keys/relation.rb +193 -118
- data/lib/composite_primary_keys/version.rb +8 -8
- data/lib/composite_primary_keys.rb +117 -118
- data/test/abstract_unit.rb +114 -113
- data/test/connections/databases.ci.yml +22 -19
- data/test/fixtures/article.rb +4 -0
- data/test/fixtures/articles.yml +4 -3
- data/test/fixtures/comment.rb +1 -3
- data/test/fixtures/comments.yml +10 -9
- data/test/fixtures/db_definitions/db2-create-tables.sql +112 -126
- data/test/fixtures/db_definitions/db2-drop-tables.sql +17 -19
- data/test/fixtures/db_definitions/mysql.sql +180 -217
- data/test/fixtures/db_definitions/oracle.drop.sql +42 -48
- data/test/fixtures/db_definitions/oracle.sql +200 -236
- data/test/fixtures/db_definitions/postgresql.sql +183 -220
- data/test/fixtures/db_definitions/sqlite.sql +170 -206
- data/test/fixtures/db_definitions/sqlserver.sql +176 -212
- data/test/fixtures/department.rb +16 -11
- data/test/fixtures/departments.yml +15 -15
- data/test/fixtures/employees.yml +27 -27
- data/test/fixtures/readings.yml +2 -2
- data/test/fixtures/restaurants_suburbs.yml +11 -11
- data/test/fixtures/streets.yml +16 -16
- data/test/fixtures/suburbs.yml +14 -14
- data/test/fixtures/user.rb +11 -10
- data/test/test_associations.rb +358 -351
- data/test/test_attributes.rb +60 -60
- data/test/test_calculations.rb +42 -42
- data/test/test_create.rb +218 -183
- data/test/test_delete.rb +182 -179
- data/test/test_exists.rb +39 -39
- data/test/test_find.rb +164 -145
- data/test/test_habtm.rb +2 -2
- data/test/test_ids.rb +112 -116
- data/test/test_nested_attributes.rb +67 -124
- data/test/test_polymorphic.rb +29 -13
- data/test/test_preload.rb +4 -3
- data/test/test_serialize.rb +2 -2
- data/test/test_update.rb +96 -78
- metadata +4 -19
- data/test/fixtures/hack.rb +0 -5
- data/test/fixtures/hacks.yml +0 -3
- data/test/fixtures/pk_called_id.rb +0 -5
- data/test/fixtures/pk_called_ids.yml +0 -11
- data/test/fixtures/reference_code_using_composite_key_alias.rb +0 -8
- data/test/fixtures/reference_code_using_simple_key_alias.rb +0 -8
- data/test/fixtures/seat.rb +0 -5
- data/test/fixtures/seats.yml +0 -9
- data/test/fixtures/topic.rb +0 -6
- data/test/fixtures/topic_source.rb +0 -7
- data/test/test_aliases.rb +0 -18
- data/test/test_enum.rb +0 -21
- data/test/test_suite.rb +0 -35
@@ -49,14 +49,6 @@ module CompositePrimaryKeys
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def in(other)
|
53
|
-
case other
|
54
|
-
when Arel::SelectManager
|
55
|
-
Arel::Nodes::In.new(self, other.ast)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
52
|
def to_s
|
61
53
|
# Doing this makes it easier to parse Base#[](attr_name)
|
62
54
|
map { |key| Utils.escape_string_key(key.to_s) }.join(ID_SEP)
|
@@ -1,17 +1,37 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module ConnectionAdapters
|
3
|
-
module DatabaseStatements
|
4
|
-
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
5
|
-
sql, binds = to_sql_and_binds(arel, binds)
|
6
|
-
value = exec_insert(sql, name, binds, pk, sequence_name)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module DatabaseStatements
|
4
|
+
def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [])
|
5
|
+
sql, binds = to_sql_and_binds(arel, binds)
|
6
|
+
value = exec_insert(sql, name, binds, pk, sequence_name)
|
7
|
+
|
8
|
+
return id_value if id_value
|
9
|
+
|
10
|
+
if pk.is_a?(Array) && !value.empty?
|
11
|
+
# This is a CPK model and the query result is not empty. Thus we can figure out the new ids for each
|
12
|
+
# auto incremented field
|
13
|
+
pk.map {|key| value.first[key]}
|
14
|
+
elsif pk.is_a?(Array)
|
15
|
+
# This is CPK, but we don't know what autoincremented fields were updated.
|
16
|
+
result = Array.new(pk.size)
|
17
|
+
|
18
|
+
# Is there an autoincrementing field?
|
19
|
+
auto_key = pk.find do |key|
|
20
|
+
attribute = arel.ast.relation[key]
|
21
|
+
column = column_for_attribute(attribute)
|
22
|
+
if column.respond_to?(:auto_increment?)
|
23
|
+
column.auto_increment?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if auto_key
|
28
|
+
result[pk.index(auto_key)] = last_inserted_id(value)
|
29
|
+
end
|
30
|
+
result
|
31
|
+
else
|
32
|
+
last_inserted_id(value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,23 +1,44 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module ConnectionAdapters
|
3
|
-
module SQLServer
|
4
|
-
module DatabaseStatements
|
5
|
-
def sql_for_insert(sql, pk, binds)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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 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,81 +1,82 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Persistence
|
3
|
-
module ClassMethods
|
4
|
-
def delete(id_or_array)
|
5
|
-
# CPK
|
6
|
-
if self.composite?
|
7
|
-
id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
|
8
|
-
[id_or_array]
|
9
|
-
else
|
10
|
-
Array(id_or_array)
|
11
|
-
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
dm.
|
54
|
-
dm.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
1
|
+
module ActiveRecord
|
2
|
+
module Persistence
|
3
|
+
module ClassMethods
|
4
|
+
def delete(id_or_array)
|
5
|
+
# CPK
|
6
|
+
if self.composite?
|
7
|
+
id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
|
8
|
+
[id_or_array]
|
9
|
+
else
|
10
|
+
Array(id_or_array)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Delete should return the number of deleted records
|
14
|
+
id_or_array.map do |id|
|
15
|
+
# Is the passed in id actually a record?
|
16
|
+
id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
|
17
|
+
delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
|
18
|
+
end.sum
|
19
|
+
else
|
20
|
+
delete_by(primary_key => id_or_array)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def _update_record(values, constraints) # :nodoc:
|
25
|
+
# CPK
|
26
|
+
if self.composite? && constraints[primary_key]
|
27
|
+
primary_key_values = constraints.delete(primary_key)
|
28
|
+
primary_key.each_with_index do |key, i|
|
29
|
+
constraints[key] = primary_key_values[i]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
34
|
+
|
35
|
+
um = arel_table.where(
|
36
|
+
constraints.reduce(&:and)
|
37
|
+
).compile_update(_substitute_values(values), primary_key)
|
38
|
+
|
39
|
+
connection.update(um, "#{self} Update")
|
40
|
+
end
|
41
|
+
|
42
|
+
def _delete_record(constraints) # :nodoc:
|
43
|
+
# CPK
|
44
|
+
if self.composite? && constraints[primary_key]
|
45
|
+
primary_key_values = constraints.delete(primary_key)
|
46
|
+
primary_key.each_with_index do |key, i|
|
47
|
+
constraints[key] = primary_key_values[i]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
constraints = _substitute_values(constraints).map { |attr, bind| attr.eq(bind) }
|
52
|
+
|
53
|
+
dm = Arel::DeleteManager.new
|
54
|
+
dm.from(arel_table)
|
55
|
+
dm.wheres = constraints
|
56
|
+
|
57
|
+
connection.delete(dm, "#{self} Destroy")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def _create_record(attribute_names = self.attribute_names)
|
62
|
+
attribute_names = attributes_for_create(attribute_names)
|
63
|
+
|
64
|
+
new_id = self.class._insert_record(
|
65
|
+
attributes_with_values(attribute_names)
|
66
|
+
)
|
67
|
+
|
68
|
+
# CPK
|
69
|
+
if self.composite?
|
70
|
+
self.id = self.id.zip(Array(new_id)).map {|id1, id2| id2.nil? ? id1 : id2}
|
71
|
+
else
|
72
|
+
self.id ||= new_id if self.class.primary_key
|
73
|
+
end
|
74
|
+
|
75
|
+
@new_record = false
|
76
|
+
|
77
|
+
yield(self) if block_given?
|
78
|
+
|
79
|
+
id
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -1,29 +1,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_keys.key
|
10
|
-
foreign_key = join_keys.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
|
-
end
|
29
|
-
end
|
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_keys.key
|
10
|
+
foreign_key = join_keys.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
|
+
end
|
29
|
+
end
|
@@ -4,7 +4,7 @@ module CompositePrimaryKeys
|
|
4
4
|
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
|
5
5
|
relation = self
|
6
6
|
unless block_given?
|
7
|
-
return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
|
7
|
+
return ::ActiveRecord::Batches::BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
|
8
8
|
end
|
9
9
|
|
10
10
|
if arel.orders.present?
|
@@ -1,81 +1,81 @@
|
|
1
|
-
module CompositePrimaryKeys
|
2
|
-
module ActiveRecord
|
3
|
-
module Calculations
|
4
|
-
def aggregate_column(column_name)
|
5
|
-
# CPK
|
6
|
-
if column_name.kind_of?(Array)
|
7
|
-
column_name.map do |column|
|
8
|
-
@klass.arel_attribute(column_name)
|
9
|
-
end
|
10
|
-
elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
|
11
|
-
@klass.arel_attribute(column_name)
|
12
|
-
else
|
13
|
-
Arel.sql(column_name == :all ? "*" : column_name.to_s)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
|
18
|
-
column_alias = column_name
|
19
|
-
|
20
|
-
# CPK
|
21
|
-
# if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
|
22
|
-
# # Shortcut when limit is zero.
|
23
|
-
# return 0 if limit_value == 0
|
24
|
-
#
|
25
|
-
# query_builder = build_count_subquery(spawn, column_name, distinct)
|
26
|
-
if operation == "count"
|
27
|
-
relation = unscope(:order)
|
28
|
-
query_builder = build_count_subquery(spawn, column_name, distinct)
|
29
|
-
else
|
30
|
-
# PostgreSQL doesn't like ORDER BY when there are no GROUP BY
|
31
|
-
relation = unscope(:order).distinct!(false)
|
32
|
-
|
33
|
-
column = aggregate_column(column_name)
|
34
|
-
|
35
|
-
select_value = operation_over_aggregate_column(column, operation, distinct)
|
36
|
-
if operation == "sum" && distinct
|
37
|
-
select_value.distinct = true
|
38
|
-
end
|
39
|
-
|
40
|
-
column_alias = select_value.alias
|
41
|
-
column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
|
42
|
-
relation.select_values = [select_value]
|
43
|
-
|
44
|
-
query_builder = relation.arel
|
45
|
-
end
|
46
|
-
|
47
|
-
result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder, nil) }
|
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
|
53
|
-
|
54
|
-
type_cast_calculated_value(value, type, operation)
|
55
|
-
end
|
56
|
-
|
57
|
-
def build_count_subquery(relation, column_name, distinct)
|
58
|
-
if column_name == :all
|
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
|
-
elsif column_name.is_a?(Array)
|
66
|
-
relation.select_values = column_name.map do |column|
|
67
|
-
Arel::Attribute.new(@klass.unscoped.table, column)
|
68
|
-
end
|
69
|
-
else
|
70
|
-
column_alias = Arel.sql("count_column")
|
71
|
-
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
|
72
|
-
end
|
73
|
-
|
74
|
-
subquery = relation.arel.as(Arel.sql("subquery_for_count"))
|
75
|
-
select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
|
76
|
-
|
77
|
-
Arel::SelectManager.new(subquery).project(select_value)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
1
|
+
module CompositePrimaryKeys
|
2
|
+
module ActiveRecord
|
3
|
+
module Calculations
|
4
|
+
def aggregate_column(column_name)
|
5
|
+
# CPK
|
6
|
+
if column_name.kind_of?(Array)
|
7
|
+
column_name.map do |column|
|
8
|
+
@klass.arel_attribute(column_name)
|
9
|
+
end
|
10
|
+
elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
|
11
|
+
@klass.arel_attribute(column_name)
|
12
|
+
else
|
13
|
+
Arel.sql(column_name == :all ? "*" : column_name.to_s)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
|
18
|
+
column_alias = column_name
|
19
|
+
|
20
|
+
# CPK
|
21
|
+
# if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
|
22
|
+
# # Shortcut when limit is zero.
|
23
|
+
# return 0 if limit_value == 0
|
24
|
+
#
|
25
|
+
# query_builder = build_count_subquery(spawn, column_name, distinct)
|
26
|
+
if operation == "count"
|
27
|
+
relation = unscope(:order)
|
28
|
+
query_builder = build_count_subquery(spawn, column_name, distinct)
|
29
|
+
else
|
30
|
+
# PostgreSQL doesn't like ORDER BY when there are no GROUP BY
|
31
|
+
relation = unscope(:order).distinct!(false)
|
32
|
+
|
33
|
+
column = aggregate_column(column_name)
|
34
|
+
|
35
|
+
select_value = operation_over_aggregate_column(column, operation, distinct)
|
36
|
+
if operation == "sum" && distinct
|
37
|
+
select_value.distinct = true
|
38
|
+
end
|
39
|
+
|
40
|
+
column_alias = select_value.alias
|
41
|
+
column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
|
42
|
+
relation.select_values = [select_value]
|
43
|
+
|
44
|
+
query_builder = relation.arel
|
45
|
+
end
|
46
|
+
|
47
|
+
result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder, nil) }
|
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
|
53
|
+
|
54
|
+
type_cast_calculated_value(value, type, operation)
|
55
|
+
end
|
56
|
+
|
57
|
+
def build_count_subquery(relation, column_name, distinct)
|
58
|
+
if column_name == :all
|
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
|
+
elsif column_name.is_a?(Array)
|
66
|
+
relation.select_values = column_name.map do |column|
|
67
|
+
Arel::Attribute.new(@klass.unscoped.table, column)
|
68
|
+
end
|
69
|
+
else
|
70
|
+
column_alias = Arel.sql("count_column")
|
71
|
+
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
|
72
|
+
end
|
73
|
+
|
74
|
+
subquery = relation.arel.as(Arel.sql("subquery_for_count"))
|
75
|
+
select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
|
76
|
+
|
77
|
+
Arel::SelectManager.new(subquery).project(select_value)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|