activerecord 5.2.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +937 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +217 -0
- data/examples/performance.rb +185 -0
- data/examples/simple.rb +15 -0
- data/lib/active_record.rb +188 -0
- data/lib/active_record/aggregations.rb +283 -0
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations.rb +1860 -0
- data/lib/active_record/associations/alias_tracker.rb +81 -0
- data/lib/active_record/associations/association.rb +299 -0
- data/lib/active_record/associations/association_scope.rb +168 -0
- data/lib/active_record/associations/belongs_to_association.rb +130 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
- data/lib/active_record/associations/builder/association.rb +140 -0
- data/lib/active_record/associations/builder/belongs_to.rb +163 -0
- data/lib/active_record/associations/builder/collection_association.rb +82 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
- data/lib/active_record/associations/builder/has_many.rb +17 -0
- data/lib/active_record/associations/builder/has_one.rb +30 -0
- data/lib/active_record/associations/builder/singular_association.rb +42 -0
- data/lib/active_record/associations/collection_association.rb +513 -0
- data/lib/active_record/associations/collection_proxy.rb +1131 -0
- data/lib/active_record/associations/foreign_association.rb +13 -0
- data/lib/active_record/associations/has_many_association.rb +144 -0
- data/lib/active_record/associations/has_many_through_association.rb +227 -0
- data/lib/active_record/associations/has_one_association.rb +120 -0
- data/lib/active_record/associations/has_one_through_association.rb +45 -0
- data/lib/active_record/associations/join_dependency.rb +262 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
- data/lib/active_record/associations/preloader.rb +193 -0
- data/lib/active_record/associations/preloader/association.rb +131 -0
- data/lib/active_record/associations/preloader/through_association.rb +107 -0
- data/lib/active_record/associations/singular_association.rb +73 -0
- data/lib/active_record/associations/through_association.rb +121 -0
- data/lib/active_record/attribute_assignment.rb +88 -0
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods.rb +492 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
- data/lib/active_record/attribute_methods/dirty.rb +150 -0
- data/lib/active_record/attribute_methods/primary_key.rb +143 -0
- data/lib/active_record/attribute_methods/query.rb +42 -0
- data/lib/active_record/attribute_methods/read.rb +85 -0
- data/lib/active_record/attribute_methods/serialization.rb +90 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
- data/lib/active_record/attribute_methods/write.rb +68 -0
- data/lib/active_record/attributes.rb +266 -0
- data/lib/active_record/autosave_association.rb +498 -0
- data/lib/active_record/base.rb +329 -0
- data/lib/active_record/callbacks.rb +353 -0
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +50 -0
- data/lib/active_record/collection_cache_key.rb +53 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
- data/lib/active_record/connection_adapters/column.rb +91 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +111 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +168 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
- data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
- data/lib/active_record/connection_handling.rb +145 -0
- data/lib/active_record/core.rb +559 -0
- data/lib/active_record/counter_cache.rb +218 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +122 -0
- data/lib/active_record/enum.rb +244 -0
- data/lib/active_record/errors.rb +380 -0
- data/lib/active_record/explain.rb +50 -0
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +34 -0
- data/lib/active_record/fixture_set/file.rb +82 -0
- data/lib/active_record/fixtures.rb +1065 -0
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +283 -0
- data/lib/active_record/integration.rb +155 -0
- data/lib/active_record/internal_metadata.rb +45 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +48 -0
- data/lib/active_record/locking/optimistic.rb +198 -0
- data/lib/active_record/locking/pessimistic.rb +89 -0
- data/lib/active_record/log_subscriber.rb +137 -0
- data/lib/active_record/migration.rb +1378 -0
- data/lib/active_record/migration/command_recorder.rb +240 -0
- data/lib/active_record/migration/compatibility.rb +217 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/model_schema.rb +521 -0
- data/lib/active_record/nested_attributes.rb +600 -0
- data/lib/active_record/no_touching.rb +58 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +763 -0
- data/lib/active_record/query_cache.rb +45 -0
- data/lib/active_record/querying.rb +70 -0
- data/lib/active_record/railtie.rb +226 -0
- data/lib/active_record/railties/console_sandbox.rb +7 -0
- data/lib/active_record/railties/controller_runtime.rb +56 -0
- data/lib/active_record/railties/databases.rake +377 -0
- data/lib/active_record/readonly_attributes.rb +24 -0
- data/lib/active_record/reflection.rb +1044 -0
- data/lib/active_record/relation.rb +629 -0
- data/lib/active_record/relation/batches.rb +287 -0
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/calculations.rb +417 -0
- data/lib/active_record/relation/delegation.rb +147 -0
- data/lib/active_record/relation/finder_methods.rb +565 -0
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +193 -0
- data/lib/active_record/relation/predicate_builder.rb +152 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
- data/lib/active_record/relation/query_attribute.rb +45 -0
- data/lib/active_record/relation/query_methods.rb +1231 -0
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +77 -0
- data/lib/active_record/relation/where_clause.rb +186 -0
- data/lib/active_record/relation/where_clause_factory.rb +34 -0
- data/lib/active_record/result.rb +149 -0
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +222 -0
- data/lib/active_record/schema.rb +70 -0
- data/lib/active_record/schema_dumper.rb +255 -0
- data/lib/active_record/schema_migration.rb +56 -0
- data/lib/active_record/scoping.rb +106 -0
- data/lib/active_record/scoping/default.rb +152 -0
- data/lib/active_record/scoping/named.rb +213 -0
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/statement_cache.rb +121 -0
- data/lib/active_record/store.rb +211 -0
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +82 -0
- data/lib/active_record/tasks/database_tasks.rb +337 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
- data/lib/active_record/timestamp.rb +153 -0
- data/lib/active_record/touch_later.rb +64 -0
- data/lib/active_record/transactions.rb +502 -0
- data/lib/active_record/translation.rb +24 -0
- data/lib/active_record/type.rb +79 -0
- data/lib/active_record/type/adapter_specific_registry.rb +136 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -0
- data/lib/active_record/validations.rb +93 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +60 -0
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +238 -0
- data/lib/active_record/version.rb +10 -0
- data/lib/rails/generators/active_record.rb +19 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration.rb +35 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +46 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
- metadata +333 -0
@@ -0,0 +1,287 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/relation/batches/batch_enumerator"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module Batches
|
7
|
+
ORDER_IGNORE_MESSAGE = "Scoped order is ignored, it's forced to be batch order."
|
8
|
+
|
9
|
+
# Looping through a collection of records from the database
|
10
|
+
# (using the Scoping::Named::ClassMethods.all method, for example)
|
11
|
+
# is very inefficient since it will try to instantiate all the objects at once.
|
12
|
+
#
|
13
|
+
# In that case, batch processing methods allow you to work
|
14
|
+
# with the records in batches, thereby greatly reducing memory consumption.
|
15
|
+
#
|
16
|
+
# The #find_each method uses #find_in_batches with a batch size of 1000 (or as
|
17
|
+
# specified by the +:batch_size+ option).
|
18
|
+
#
|
19
|
+
# Person.find_each do |person|
|
20
|
+
# person.do_awesome_stuff
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# Person.where("age > 21").find_each do |person|
|
24
|
+
# person.party_all_night!
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# If you do not provide a block to #find_each, it will return an Enumerator
|
28
|
+
# for chaining with other methods:
|
29
|
+
#
|
30
|
+
# Person.find_each.with_index do |person, index|
|
31
|
+
# person.award_trophy(index + 1)
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# ==== Options
|
35
|
+
# * <tt>:batch_size</tt> - Specifies the size of the batch. Defaults to 1000.
|
36
|
+
# * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
|
37
|
+
# * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
|
38
|
+
# * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
|
39
|
+
# an order is present in the relation.
|
40
|
+
#
|
41
|
+
# Limits are honored, and if present there is no requirement for the batch
|
42
|
+
# size: it can be less than, equal to, or greater than the limit.
|
43
|
+
#
|
44
|
+
# The options +start+ and +finish+ are especially useful if you want
|
45
|
+
# multiple workers dealing with the same processing queue. You can make
|
46
|
+
# worker 1 handle all the records between id 1 and 9999 and worker 2
|
47
|
+
# handle from 10000 and beyond by setting the +:start+ and +:finish+
|
48
|
+
# option on each worker.
|
49
|
+
#
|
50
|
+
# # In worker 1, let's process until 9999 records.
|
51
|
+
# Person.find_each(finish: 9_999) do |person|
|
52
|
+
# person.party_all_night!
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# # In worker 2, let's process from record 10_000 and onwards.
|
56
|
+
# Person.find_each(start: 10_000) do |person|
|
57
|
+
# person.party_all_night!
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# NOTE: It's not possible to set the order. That is automatically set to
|
61
|
+
# ascending on the primary key ("id ASC") to make the batch ordering
|
62
|
+
# work. This also means that this method only works when the primary key is
|
63
|
+
# orderable (e.g. an integer or string).
|
64
|
+
#
|
65
|
+
# NOTE: By its nature, batch processing is subject to race conditions if
|
66
|
+
# other processes are modifying the database.
|
67
|
+
def find_each(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil)
|
68
|
+
if block_given?
|
69
|
+
find_in_batches(start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do |records|
|
70
|
+
records.each { |record| yield record }
|
71
|
+
end
|
72
|
+
else
|
73
|
+
enum_for(:find_each, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do
|
74
|
+
relation = self
|
75
|
+
apply_limits(relation, start, finish).size
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Yields each batch of records that was found by the find options as
|
81
|
+
# an array.
|
82
|
+
#
|
83
|
+
# Person.where("age > 21").find_in_batches do |group|
|
84
|
+
# sleep(50) # Make sure it doesn't get too crowded in there!
|
85
|
+
# group.each { |person| person.party_all_night! }
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# If you do not provide a block to #find_in_batches, it will return an Enumerator
|
89
|
+
# for chaining with other methods:
|
90
|
+
#
|
91
|
+
# Person.find_in_batches.with_index do |group, batch|
|
92
|
+
# puts "Processing group ##{batch}"
|
93
|
+
# group.each(&:recover_from_last_night!)
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# To be yielded each record one by one, use #find_each instead.
|
97
|
+
#
|
98
|
+
# ==== Options
|
99
|
+
# * <tt>:batch_size</tt> - Specifies the size of the batch. Defaults to 1000.
|
100
|
+
# * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
|
101
|
+
# * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
|
102
|
+
# * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
|
103
|
+
# an order is present in the relation.
|
104
|
+
#
|
105
|
+
# Limits are honored, and if present there is no requirement for the batch
|
106
|
+
# size: it can be less than, equal to, or greater than the limit.
|
107
|
+
#
|
108
|
+
# The options +start+ and +finish+ are especially useful if you want
|
109
|
+
# multiple workers dealing with the same processing queue. You can make
|
110
|
+
# worker 1 handle all the records between id 1 and 9999 and worker 2
|
111
|
+
# handle from 10000 and beyond by setting the +:start+ and +:finish+
|
112
|
+
# option on each worker.
|
113
|
+
#
|
114
|
+
# # Let's process from record 10_000 on.
|
115
|
+
# Person.find_in_batches(start: 10_000) do |group|
|
116
|
+
# group.each { |person| person.party_all_night! }
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# NOTE: It's not possible to set the order. That is automatically set to
|
120
|
+
# ascending on the primary key ("id ASC") to make the batch ordering
|
121
|
+
# work. This also means that this method only works when the primary key is
|
122
|
+
# orderable (e.g. an integer or string).
|
123
|
+
#
|
124
|
+
# NOTE: By its nature, batch processing is subject to race conditions if
|
125
|
+
# other processes are modifying the database.
|
126
|
+
def find_in_batches(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil)
|
127
|
+
relation = self
|
128
|
+
unless block_given?
|
129
|
+
return to_enum(:find_in_batches, start: start, finish: finish, batch_size: batch_size, error_on_ignore: error_on_ignore) do
|
130
|
+
total = apply_limits(relation, start, finish).size
|
131
|
+
(total - 1).div(batch_size) + 1
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
in_batches(of: batch_size, start: start, finish: finish, load: true, error_on_ignore: error_on_ignore) do |batch|
|
136
|
+
yield batch.to_a
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Yields ActiveRecord::Relation objects to work with a batch of records.
|
141
|
+
#
|
142
|
+
# Person.where("age > 21").in_batches do |relation|
|
143
|
+
# relation.delete_all
|
144
|
+
# sleep(10) # Throttle the delete queries
|
145
|
+
# end
|
146
|
+
#
|
147
|
+
# If you do not provide a block to #in_batches, it will return a
|
148
|
+
# BatchEnumerator which is enumerable.
|
149
|
+
#
|
150
|
+
# Person.in_batches.each_with_index do |relation, batch_index|
|
151
|
+
# puts "Processing relation ##{batch_index}"
|
152
|
+
# relation.delete_all
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# Examples of calling methods on the returned BatchEnumerator object:
|
156
|
+
#
|
157
|
+
# Person.in_batches.delete_all
|
158
|
+
# Person.in_batches.update_all(awesome: true)
|
159
|
+
# Person.in_batches.each_record(&:party_all_night!)
|
160
|
+
#
|
161
|
+
# ==== Options
|
162
|
+
# * <tt>:of</tt> - Specifies the size of the batch. Defaults to 1000.
|
163
|
+
# * <tt>:load</tt> - Specifies if the relation should be loaded. Defaults to false.
|
164
|
+
# * <tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.
|
165
|
+
# * <tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.
|
166
|
+
# * <tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when
|
167
|
+
# an order is present in the relation.
|
168
|
+
#
|
169
|
+
# Limits are honored, and if present there is no requirement for the batch
|
170
|
+
# size, it can be less than, equal, or greater than the limit.
|
171
|
+
#
|
172
|
+
# The options +start+ and +finish+ are especially useful if you want
|
173
|
+
# multiple workers dealing with the same processing queue. You can make
|
174
|
+
# worker 1 handle all the records between id 1 and 9999 and worker 2
|
175
|
+
# handle from 10000 and beyond by setting the +:start+ and +:finish+
|
176
|
+
# option on each worker.
|
177
|
+
#
|
178
|
+
# # Let's process from record 10_000 on.
|
179
|
+
# Person.in_batches(start: 10_000).update_all(awesome: true)
|
180
|
+
#
|
181
|
+
# An example of calling where query method on the relation:
|
182
|
+
#
|
183
|
+
# Person.in_batches.each do |relation|
|
184
|
+
# relation.update_all('age = age + 1')
|
185
|
+
# relation.where('age > 21').update_all(should_party: true)
|
186
|
+
# relation.where('age <= 21').delete_all
|
187
|
+
# end
|
188
|
+
#
|
189
|
+
# NOTE: If you are going to iterate through each record, you should call
|
190
|
+
# #each_record on the yielded BatchEnumerator:
|
191
|
+
#
|
192
|
+
# Person.in_batches.each_record(&:party_all_night!)
|
193
|
+
#
|
194
|
+
# NOTE: It's not possible to set the order. That is automatically set to
|
195
|
+
# ascending on the primary key ("id ASC") to make the batch ordering
|
196
|
+
# consistent. Therefore the primary key must be orderable, e.g. an integer
|
197
|
+
# or a string.
|
198
|
+
#
|
199
|
+
# NOTE: By its nature, batch processing is subject to race conditions if
|
200
|
+
# other processes are modifying the database.
|
201
|
+
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
|
202
|
+
relation = self
|
203
|
+
unless block_given?
|
204
|
+
return BatchEnumerator.new(of: of, start: start, finish: finish, relation: self)
|
205
|
+
end
|
206
|
+
|
207
|
+
if arel.orders.present?
|
208
|
+
act_on_ignored_order(error_on_ignore)
|
209
|
+
end
|
210
|
+
|
211
|
+
batch_limit = of
|
212
|
+
if limit_value
|
213
|
+
remaining = limit_value
|
214
|
+
batch_limit = remaining if remaining < batch_limit
|
215
|
+
end
|
216
|
+
|
217
|
+
relation = relation.reorder(batch_order).limit(batch_limit)
|
218
|
+
relation = apply_limits(relation, start, finish)
|
219
|
+
relation.skip_query_cache! # Retaining the results in the query cache would undermine the point of batching
|
220
|
+
batch_relation = relation
|
221
|
+
|
222
|
+
loop do
|
223
|
+
if load
|
224
|
+
records = batch_relation.records
|
225
|
+
ids = records.map(&:id)
|
226
|
+
yielded_relation = where(primary_key => ids)
|
227
|
+
yielded_relation.load_records(records)
|
228
|
+
else
|
229
|
+
ids = batch_relation.pluck(primary_key)
|
230
|
+
yielded_relation = where(primary_key => ids)
|
231
|
+
end
|
232
|
+
|
233
|
+
break if ids.empty?
|
234
|
+
|
235
|
+
primary_key_offset = ids.last
|
236
|
+
raise ArgumentError.new("Primary key not included in the custom select clause") unless primary_key_offset
|
237
|
+
|
238
|
+
yield yielded_relation
|
239
|
+
|
240
|
+
break if ids.length < batch_limit
|
241
|
+
|
242
|
+
if limit_value
|
243
|
+
remaining -= ids.length
|
244
|
+
|
245
|
+
if remaining == 0
|
246
|
+
# Saves a useless iteration when the limit is a multiple of the
|
247
|
+
# batch size.
|
248
|
+
break
|
249
|
+
elsif remaining < batch_limit
|
250
|
+
relation = relation.limit(remaining)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
attr = Relation::QueryAttribute.new(primary_key, primary_key_offset, klass.type_for_attribute(primary_key))
|
255
|
+
batch_relation = relation.where(arel_attribute(primary_key).gt(Arel::Nodes::BindParam.new(attr)))
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
private
|
260
|
+
|
261
|
+
def apply_limits(relation, start, finish)
|
262
|
+
if start
|
263
|
+
attr = Relation::QueryAttribute.new(primary_key, start, klass.type_for_attribute(primary_key))
|
264
|
+
relation = relation.where(arel_attribute(primary_key).gteq(Arel::Nodes::BindParam.new(attr)))
|
265
|
+
end
|
266
|
+
if finish
|
267
|
+
attr = Relation::QueryAttribute.new(primary_key, finish, klass.type_for_attribute(primary_key))
|
268
|
+
relation = relation.where(arel_attribute(primary_key).lteq(Arel::Nodes::BindParam.new(attr)))
|
269
|
+
end
|
270
|
+
relation
|
271
|
+
end
|
272
|
+
|
273
|
+
def batch_order
|
274
|
+
arel_attribute(primary_key).asc
|
275
|
+
end
|
276
|
+
|
277
|
+
def act_on_ignored_order(error_on_ignore)
|
278
|
+
raise_error = (error_on_ignore.nil? ? klass.error_on_ignored_order : error_on_ignore)
|
279
|
+
|
280
|
+
if raise_error
|
281
|
+
raise ArgumentError.new(ORDER_IGNORE_MESSAGE)
|
282
|
+
elsif logger
|
283
|
+
logger.warn(ORDER_IGNORE_MESSAGE)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Batches
|
5
|
+
class BatchEnumerator
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(of: 1000, start: nil, finish: nil, relation:) #:nodoc:
|
9
|
+
@of = of
|
10
|
+
@relation = relation
|
11
|
+
@start = start
|
12
|
+
@finish = finish
|
13
|
+
end
|
14
|
+
|
15
|
+
# Looping through a collection of records from the database (using the
|
16
|
+
# +all+ method, for example) is very inefficient since it will try to
|
17
|
+
# instantiate all the objects at once.
|
18
|
+
#
|
19
|
+
# In that case, batch processing methods allow you to work with the
|
20
|
+
# records in batches, thereby greatly reducing memory consumption.
|
21
|
+
#
|
22
|
+
# Person.in_batches.each_record do |person|
|
23
|
+
# person.do_awesome_stuff
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Person.where("age > 21").in_batches(of: 10).each_record do |person|
|
27
|
+
# person.party_all_night!
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# If you do not provide a block to #each_record, it will return an Enumerator
|
31
|
+
# for chaining with other methods:
|
32
|
+
#
|
33
|
+
# Person.in_batches.each_record.with_index do |person, index|
|
34
|
+
# person.award_trophy(index + 1)
|
35
|
+
# end
|
36
|
+
def each_record
|
37
|
+
return to_enum(:each_record) unless block_given?
|
38
|
+
|
39
|
+
@relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: true).each do |relation|
|
40
|
+
relation.records.each { |record| yield record }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Delegates #delete_all, #update_all, #destroy_all methods to each batch.
|
45
|
+
#
|
46
|
+
# People.in_batches.delete_all
|
47
|
+
# People.where('age < 10').in_batches.destroy_all
|
48
|
+
# People.in_batches.update_all('age = age + 1')
|
49
|
+
[:delete_all, :update_all, :destroy_all].each do |method|
|
50
|
+
define_method(method) do |*args, &block|
|
51
|
+
@relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false).each do |relation|
|
52
|
+
relation.send(method, *args, &block)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Yields an ActiveRecord::Relation object for each batch of records.
|
58
|
+
#
|
59
|
+
# Person.in_batches.each do |relation|
|
60
|
+
# relation.update_all(awesome: true)
|
61
|
+
# end
|
62
|
+
def each
|
63
|
+
enum = @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false)
|
64
|
+
return enum.each { |relation| yield relation } if block_given?
|
65
|
+
enum
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,417 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Calculations
|
5
|
+
# Count the records.
|
6
|
+
#
|
7
|
+
# Person.count
|
8
|
+
# # => the total count of all people
|
9
|
+
#
|
10
|
+
# Person.count(:age)
|
11
|
+
# # => returns the total count of all people whose age is present in database
|
12
|
+
#
|
13
|
+
# Person.count(:all)
|
14
|
+
# # => performs a COUNT(*) (:all is an alias for '*')
|
15
|
+
#
|
16
|
+
# Person.distinct.count(:age)
|
17
|
+
# # => counts the number of different age values
|
18
|
+
#
|
19
|
+
# If #count is used with {Relation#group}[rdoc-ref:QueryMethods#group],
|
20
|
+
# it returns a Hash whose keys represent the aggregated column,
|
21
|
+
# and the values are the respective amounts:
|
22
|
+
#
|
23
|
+
# Person.group(:city).count
|
24
|
+
# # => { 'Rome' => 5, 'Paris' => 3 }
|
25
|
+
#
|
26
|
+
# If #count is used with {Relation#group}[rdoc-ref:QueryMethods#group] for multiple columns, it returns a Hash whose
|
27
|
+
# keys are an array containing the individual values of each column and the value
|
28
|
+
# of each key would be the #count.
|
29
|
+
#
|
30
|
+
# Article.group(:status, :category).count
|
31
|
+
# # => {["draft", "business"]=>10, ["draft", "technology"]=>4,
|
32
|
+
# ["published", "business"]=>0, ["published", "technology"]=>2}
|
33
|
+
#
|
34
|
+
# If #count is used with {Relation#select}[rdoc-ref:QueryMethods#select], it will count the selected columns:
|
35
|
+
#
|
36
|
+
# Person.select(:age).count
|
37
|
+
# # => counts the number of different age values
|
38
|
+
#
|
39
|
+
# Note: not all valid {Relation#select}[rdoc-ref:QueryMethods#select] expressions are valid #count expressions. The specifics differ
|
40
|
+
# between databases. In invalid cases, an error from the database is thrown.
|
41
|
+
def count(column_name = nil)
|
42
|
+
if block_given?
|
43
|
+
unless column_name.nil?
|
44
|
+
ActiveSupport::Deprecation.warn \
|
45
|
+
"When `count' is called with a block, it ignores other arguments. " \
|
46
|
+
"This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
|
47
|
+
end
|
48
|
+
|
49
|
+
return super()
|
50
|
+
end
|
51
|
+
|
52
|
+
calculate(:count, column_name)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Calculates the average value on a given column. Returns +nil+ if there's
|
56
|
+
# no row. See #calculate for examples with options.
|
57
|
+
#
|
58
|
+
# Person.average(:age) # => 35.8
|
59
|
+
def average(column_name)
|
60
|
+
calculate(:average, column_name)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Calculates the minimum value on a given column. The value is returned
|
64
|
+
# with the same data type of the column, or +nil+ if there's no row. See
|
65
|
+
# #calculate for examples with options.
|
66
|
+
#
|
67
|
+
# Person.minimum(:age) # => 7
|
68
|
+
def minimum(column_name)
|
69
|
+
calculate(:minimum, column_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Calculates the maximum value on a given column. The value is returned
|
73
|
+
# with the same data type of the column, or +nil+ if there's no row. See
|
74
|
+
# #calculate for examples with options.
|
75
|
+
#
|
76
|
+
# Person.maximum(:age) # => 93
|
77
|
+
def maximum(column_name)
|
78
|
+
calculate(:maximum, column_name)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Calculates the sum of values on a given column. The value is returned
|
82
|
+
# with the same data type of the column, +0+ if there's no row. See
|
83
|
+
# #calculate for examples with options.
|
84
|
+
#
|
85
|
+
# Person.sum(:age) # => 4562
|
86
|
+
def sum(column_name = nil)
|
87
|
+
if block_given?
|
88
|
+
unless column_name.nil?
|
89
|
+
ActiveSupport::Deprecation.warn \
|
90
|
+
"When `sum' is called with a block, it ignores other arguments. " \
|
91
|
+
"This behavior is now deprecated and will result in an ArgumentError in Rails 6.0."
|
92
|
+
end
|
93
|
+
|
94
|
+
return super()
|
95
|
+
end
|
96
|
+
|
97
|
+
calculate(:sum, column_name)
|
98
|
+
end
|
99
|
+
|
100
|
+
# This calculates aggregate values in the given column. Methods for #count, #sum, #average,
|
101
|
+
# #minimum, and #maximum have been added as shortcuts.
|
102
|
+
#
|
103
|
+
# Person.calculate(:count, :all) # The same as Person.count
|
104
|
+
# Person.average(:age) # SELECT AVG(age) FROM people...
|
105
|
+
#
|
106
|
+
# # Selects the minimum age for any family without any minors
|
107
|
+
# Person.group(:last_name).having("min(age) > 17").minimum(:age)
|
108
|
+
#
|
109
|
+
# Person.sum("2 * age")
|
110
|
+
#
|
111
|
+
# There are two basic forms of output:
|
112
|
+
#
|
113
|
+
# * Single aggregate value: The single value is type cast to Integer for COUNT, Float
|
114
|
+
# for AVG, and the given column's type for everything else.
|
115
|
+
#
|
116
|
+
# * Grouped values: This returns an ordered hash of the values and groups them. It
|
117
|
+
# takes either a column name, or the name of a belongs_to association.
|
118
|
+
#
|
119
|
+
# values = Person.group('last_name').maximum(:age)
|
120
|
+
# puts values["Drake"]
|
121
|
+
# # => 43
|
122
|
+
#
|
123
|
+
# drake = Family.find_by(last_name: 'Drake')
|
124
|
+
# values = Person.group(:family).maximum(:age) # Person belongs_to :family
|
125
|
+
# puts values[drake]
|
126
|
+
# # => 43
|
127
|
+
#
|
128
|
+
# values.each do |family, max_age|
|
129
|
+
# ...
|
130
|
+
# end
|
131
|
+
def calculate(operation, column_name)
|
132
|
+
if has_include?(column_name)
|
133
|
+
relation = apply_join_dependency
|
134
|
+
|
135
|
+
if operation.to_s.downcase == "count"
|
136
|
+
relation.distinct!
|
137
|
+
# PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
|
138
|
+
if (column_name == :all || column_name.nil?) && select_values.empty?
|
139
|
+
relation.order_values = []
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
relation.calculate(operation, column_name)
|
144
|
+
else
|
145
|
+
perform_calculation(operation, column_name)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Use #pluck as a shortcut to select one or more attributes without
|
150
|
+
# loading a bunch of records just to grab the attributes you want.
|
151
|
+
#
|
152
|
+
# Person.pluck(:name)
|
153
|
+
#
|
154
|
+
# instead of
|
155
|
+
#
|
156
|
+
# Person.all.map(&:name)
|
157
|
+
#
|
158
|
+
# Pluck returns an Array of attribute values type-casted to match
|
159
|
+
# the plucked column names, if they can be deduced. Plucking an SQL fragment
|
160
|
+
# returns String values by default.
|
161
|
+
#
|
162
|
+
# Person.pluck(:name)
|
163
|
+
# # SELECT people.name FROM people
|
164
|
+
# # => ['David', 'Jeremy', 'Jose']
|
165
|
+
#
|
166
|
+
# Person.pluck(:id, :name)
|
167
|
+
# # SELECT people.id, people.name FROM people
|
168
|
+
# # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
|
169
|
+
#
|
170
|
+
# Person.distinct.pluck(:role)
|
171
|
+
# # SELECT DISTINCT role FROM people
|
172
|
+
# # => ['admin', 'member', 'guest']
|
173
|
+
#
|
174
|
+
# Person.where(age: 21).limit(5).pluck(:id)
|
175
|
+
# # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
|
176
|
+
# # => [2, 3]
|
177
|
+
#
|
178
|
+
# Person.pluck('DATEDIFF(updated_at, created_at)')
|
179
|
+
# # SELECT DATEDIFF(updated_at, created_at) FROM people
|
180
|
+
# # => ['0', '27761', '173']
|
181
|
+
#
|
182
|
+
# See also #ids.
|
183
|
+
#
|
184
|
+
def pluck(*column_names)
|
185
|
+
if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
|
186
|
+
return records.pluck(*column_names)
|
187
|
+
end
|
188
|
+
|
189
|
+
if has_include?(column_names.first)
|
190
|
+
relation = apply_join_dependency
|
191
|
+
relation.pluck(*column_names)
|
192
|
+
else
|
193
|
+
klass.enforce_raw_sql_whitelist(column_names)
|
194
|
+
relation = spawn
|
195
|
+
relation.select_values = column_names
|
196
|
+
result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
|
197
|
+
result.cast_values(klass.attribute_types)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Pluck all the ID's for the relation using the table's primary key
|
202
|
+
#
|
203
|
+
# Person.ids # SELECT people.id FROM people
|
204
|
+
# Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.person_id = people.id
|
205
|
+
def ids
|
206
|
+
pluck primary_key
|
207
|
+
end
|
208
|
+
|
209
|
+
private
|
210
|
+
def has_include?(column_name)
|
211
|
+
eager_loading? || (includes_values.present? && column_name && column_name != :all)
|
212
|
+
end
|
213
|
+
|
214
|
+
def perform_calculation(operation, column_name)
|
215
|
+
operation = operation.to_s.downcase
|
216
|
+
|
217
|
+
# If #count is used with #distinct (i.e. `relation.distinct.count`) it is
|
218
|
+
# considered distinct.
|
219
|
+
distinct = distinct_value
|
220
|
+
|
221
|
+
if operation == "count"
|
222
|
+
column_name ||= select_for_count
|
223
|
+
if column_name == :all
|
224
|
+
if !distinct
|
225
|
+
distinct = distinct_select?(select_for_count) if group_values.empty?
|
226
|
+
elsif group_values.any? || select_values.empty? && order_values.empty?
|
227
|
+
column_name = primary_key
|
228
|
+
end
|
229
|
+
elsif distinct_select?(column_name)
|
230
|
+
distinct = nil
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
if group_values.any?
|
235
|
+
execute_grouped_calculation(operation, column_name, distinct)
|
236
|
+
else
|
237
|
+
execute_simple_calculation(operation, column_name, distinct)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def distinct_select?(column_name)
|
242
|
+
column_name.is_a?(::String) && /\bDISTINCT[\s(]/i.match?(column_name)
|
243
|
+
end
|
244
|
+
|
245
|
+
def aggregate_column(column_name)
|
246
|
+
return column_name if Arel::Expressions === column_name
|
247
|
+
|
248
|
+
if @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
|
249
|
+
@klass.arel_attribute(column_name)
|
250
|
+
else
|
251
|
+
Arel.sql(column_name == :all ? "*" : column_name.to_s)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def operation_over_aggregate_column(column, operation, distinct)
|
256
|
+
operation == "count" ? column.count(distinct) : column.send(operation)
|
257
|
+
end
|
258
|
+
|
259
|
+
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
|
260
|
+
column_alias = column_name
|
261
|
+
|
262
|
+
if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
|
263
|
+
# Shortcut when limit is zero.
|
264
|
+
return 0 if limit_value == 0
|
265
|
+
|
266
|
+
query_builder = build_count_subquery(spawn, column_name, distinct)
|
267
|
+
else
|
268
|
+
# PostgreSQL doesn't like ORDER BY when there are no GROUP BY
|
269
|
+
relation = unscope(:order).distinct!(false)
|
270
|
+
|
271
|
+
column = aggregate_column(column_name)
|
272
|
+
|
273
|
+
select_value = operation_over_aggregate_column(column, operation, distinct)
|
274
|
+
if operation == "sum" && distinct
|
275
|
+
select_value.distinct = true
|
276
|
+
end
|
277
|
+
|
278
|
+
column_alias = select_value.alias
|
279
|
+
column_alias ||= @klass.connection.column_name_for_operation(operation, select_value)
|
280
|
+
relation.select_values = [select_value]
|
281
|
+
|
282
|
+
query_builder = relation.arel
|
283
|
+
end
|
284
|
+
|
285
|
+
result = skip_query_cache_if_necessary { @klass.connection.select_all(query_builder, nil) }
|
286
|
+
row = result.first
|
287
|
+
value = row && row.values.first
|
288
|
+
type = result.column_types.fetch(column_alias) do
|
289
|
+
type_for(column_name)
|
290
|
+
end
|
291
|
+
|
292
|
+
type_cast_calculated_value(value, type, operation)
|
293
|
+
end
|
294
|
+
|
295
|
+
def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
|
296
|
+
group_attrs = group_values
|
297
|
+
|
298
|
+
if group_attrs.first.respond_to?(:to_sym)
|
299
|
+
association = @klass._reflect_on_association(group_attrs.first)
|
300
|
+
associated = group_attrs.size == 1 && association && association.belongs_to? # only count belongs_to associations
|
301
|
+
group_fields = Array(associated ? association.foreign_key : group_attrs)
|
302
|
+
else
|
303
|
+
group_fields = group_attrs
|
304
|
+
end
|
305
|
+
group_fields = arel_columns(group_fields)
|
306
|
+
|
307
|
+
group_aliases = group_fields.map { |field| column_alias_for(field) }
|
308
|
+
group_columns = group_aliases.zip(group_fields)
|
309
|
+
|
310
|
+
if operation == "count" && column_name == :all
|
311
|
+
aggregate_alias = "count_all"
|
312
|
+
else
|
313
|
+
aggregate_alias = column_alias_for([operation, column_name].join(" "))
|
314
|
+
end
|
315
|
+
|
316
|
+
select_values = [
|
317
|
+
operation_over_aggregate_column(
|
318
|
+
aggregate_column(column_name),
|
319
|
+
operation,
|
320
|
+
distinct).as(aggregate_alias)
|
321
|
+
]
|
322
|
+
select_values += self.select_values unless having_clause.empty?
|
323
|
+
|
324
|
+
select_values.concat group_columns.map { |aliaz, field|
|
325
|
+
if field.respond_to?(:as)
|
326
|
+
field.as(aliaz)
|
327
|
+
else
|
328
|
+
"#{field} AS #{aliaz}"
|
329
|
+
end
|
330
|
+
}
|
331
|
+
|
332
|
+
relation = except(:group).distinct!(false)
|
333
|
+
relation.group_values = group_fields
|
334
|
+
relation.select_values = select_values
|
335
|
+
|
336
|
+
calculated_data = skip_query_cache_if_necessary { @klass.connection.select_all(relation.arel, nil) }
|
337
|
+
|
338
|
+
if association
|
339
|
+
key_ids = calculated_data.collect { |row| row[group_aliases.first] }
|
340
|
+
key_records = association.klass.base_class.where(association.klass.base_class.primary_key => key_ids)
|
341
|
+
key_records = Hash[key_records.map { |r| [r.id, r] }]
|
342
|
+
end
|
343
|
+
|
344
|
+
Hash[calculated_data.map do |row|
|
345
|
+
key = group_columns.map { |aliaz, col_name|
|
346
|
+
type = type_for(col_name) do
|
347
|
+
calculated_data.column_types.fetch(aliaz, Type.default_value)
|
348
|
+
end
|
349
|
+
type_cast_calculated_value(row[aliaz], type)
|
350
|
+
}
|
351
|
+
key = key.first if key.size == 1
|
352
|
+
key = key_records[key] if associated
|
353
|
+
|
354
|
+
type = calculated_data.column_types.fetch(aggregate_alias) { type_for(column_name) }
|
355
|
+
[key, type_cast_calculated_value(row[aggregate_alias], type, operation)]
|
356
|
+
end]
|
357
|
+
end
|
358
|
+
|
359
|
+
# Converts the given keys to the value that the database adapter returns as
|
360
|
+
# a usable column name:
|
361
|
+
#
|
362
|
+
# column_alias_for("users.id") # => "users_id"
|
363
|
+
# column_alias_for("sum(id)") # => "sum_id"
|
364
|
+
# column_alias_for("count(distinct users.id)") # => "count_distinct_users_id"
|
365
|
+
# column_alias_for("count(*)") # => "count_all"
|
366
|
+
def column_alias_for(keys)
|
367
|
+
if keys.respond_to? :name
|
368
|
+
keys = "#{keys.relation.name}.#{keys.name}"
|
369
|
+
end
|
370
|
+
|
371
|
+
table_name = keys.to_s.downcase
|
372
|
+
table_name.gsub!(/\*/, "all")
|
373
|
+
table_name.gsub!(/\W+/, " ")
|
374
|
+
table_name.strip!
|
375
|
+
table_name.gsub!(/ +/, "_")
|
376
|
+
|
377
|
+
@klass.connection.table_alias_for(table_name)
|
378
|
+
end
|
379
|
+
|
380
|
+
def type_for(field, &block)
|
381
|
+
field_name = field.respond_to?(:name) ? field.name.to_s : field.to_s.split(".").last
|
382
|
+
@klass.type_for_attribute(field_name, &block)
|
383
|
+
end
|
384
|
+
|
385
|
+
def type_cast_calculated_value(value, type, operation = nil)
|
386
|
+
case operation
|
387
|
+
when "count" then value.to_i
|
388
|
+
when "sum" then type.deserialize(value || 0)
|
389
|
+
when "average" then value && value.respond_to?(:to_d) ? value.to_d : value
|
390
|
+
else type.deserialize(value)
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
def select_for_count
|
395
|
+
if select_values.present?
|
396
|
+
return select_values.first if select_values.one?
|
397
|
+
select_values.join(", ")
|
398
|
+
else
|
399
|
+
:all
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
def build_count_subquery(relation, column_name, distinct)
|
404
|
+
if column_name == :all
|
405
|
+
relation.select_values = [ Arel.sql(FinderMethods::ONE_AS_ONE) ] unless distinct
|
406
|
+
else
|
407
|
+
column_alias = Arel.sql("count_column")
|
408
|
+
relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
|
409
|
+
end
|
410
|
+
|
411
|
+
subquery = relation.arel.as(Arel.sql("subquery_for_count"))
|
412
|
+
select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
|
413
|
+
|
414
|
+
Arel::SelectManager.new(subquery).project(select_value)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
end
|