activerecord 6.1.6 → 7.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1627 -983
- data/MIT-LICENSE +1 -1
- data/README.rdoc +18 -18
- data/lib/active_record/aggregations.rb +17 -14
- data/lib/active_record/association_relation.rb +1 -11
- data/lib/active_record/associations/association.rb +50 -19
- data/lib/active_record/associations/association_scope.rb +17 -12
- data/lib/active_record/associations/belongs_to_association.rb +28 -9
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
- data/lib/active_record/associations/builder/association.rb +11 -5
- data/lib/active_record/associations/builder/belongs_to.rb +40 -14
- data/lib/active_record/associations/builder/collection_association.rb +10 -3
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
- data/lib/active_record/associations/builder/has_many.rb +3 -2
- data/lib/active_record/associations/builder/has_one.rb +2 -1
- data/lib/active_record/associations/builder/singular_association.rb +6 -2
- data/lib/active_record/associations/collection_association.rb +35 -31
- data/lib/active_record/associations/collection_proxy.rb +30 -15
- data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
- data/lib/active_record/associations/foreign_association.rb +10 -3
- data/lib/active_record/associations/has_many_association.rb +28 -18
- data/lib/active_record/associations/has_many_through_association.rb +12 -7
- data/lib/active_record/associations/has_one_association.rb +20 -10
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +26 -16
- data/lib/active_record/associations/preloader/association.rb +207 -52
- data/lib/active_record/associations/preloader/batch.rb +48 -0
- data/lib/active_record/associations/preloader/branch.rb +147 -0
- data/lib/active_record/associations/preloader/through_association.rb +50 -14
- data/lib/active_record/associations/preloader.rb +50 -121
- data/lib/active_record/associations/singular_association.rb +9 -3
- data/lib/active_record/associations/through_association.rb +25 -14
- data/lib/active_record/associations.rb +439 -305
- data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
- data/lib/active_record/attribute_assignment.rb +1 -3
- data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
- data/lib/active_record/attribute_methods/dirty.rb +73 -22
- data/lib/active_record/attribute_methods/primary_key.rb +78 -26
- data/lib/active_record/attribute_methods/query.rb +31 -19
- data/lib/active_record/attribute_methods/read.rb +25 -10
- data/lib/active_record/attribute_methods/serialization.rb +194 -37
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
- data/lib/active_record/attribute_methods/write.rb +10 -13
- data/lib/active_record/attribute_methods.rb +121 -40
- data/lib/active_record/attributes.rb +27 -38
- data/lib/active_record/autosave_association.rb +61 -30
- data/lib/active_record/base.rb +25 -2
- data/lib/active_record/callbacks.rb +18 -34
- data/lib/active_record/coders/column_serializer.rb +61 -0
- data/lib/active_record/coders/json.rb +1 -1
- data/lib/active_record/coders/yaml_column.rb +70 -34
- data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +96 -590
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +77 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +360 -138
- data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
- data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -149
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +285 -156
- data/lib/active_record/connection_adapters/column.rb +13 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
- data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +38 -14
- data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +104 -53
- data/lib/active_record/connection_adapters/pool_config.rb +20 -11
- data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
- data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +394 -74
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +509 -247
- data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
- data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +294 -102
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
- data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
- data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
- data/lib/active_record/connection_adapters.rb +9 -6
- data/lib/active_record/connection_handling.rb +107 -136
- data/lib/active_record/core.rb +202 -223
- data/lib/active_record/counter_cache.rb +46 -25
- data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
- data/lib/active_record/database_configurations/database_config.rb +21 -12
- data/lib/active_record/database_configurations/hash_config.rb +84 -16
- data/lib/active_record/database_configurations/url_config.rb +18 -12
- data/lib/active_record/database_configurations.rb +95 -59
- data/lib/active_record/delegated_type.rb +61 -15
- data/lib/active_record/deprecator.rb +7 -0
- data/lib/active_record/destroy_association_async_job.rb +3 -1
- data/lib/active_record/disable_joins_association_relation.rb +39 -0
- data/lib/active_record/dynamic_matchers.rb +1 -1
- data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
- data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
- data/lib/active_record/encryption/cipher.rb +53 -0
- data/lib/active_record/encryption/config.rb +68 -0
- data/lib/active_record/encryption/configurable.rb +60 -0
- data/lib/active_record/encryption/context.rb +42 -0
- data/lib/active_record/encryption/contexts.rb +76 -0
- data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
- data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
- data/lib/active_record/encryption/encryptable_record.rb +224 -0
- data/lib/active_record/encryption/encrypted_attribute_type.rb +151 -0
- data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
- data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
- data/lib/active_record/encryption/encryptor.rb +155 -0
- data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
- data/lib/active_record/encryption/errors.rb +15 -0
- data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
- data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
- data/lib/active_record/encryption/key.rb +28 -0
- data/lib/active_record/encryption/key_generator.rb +53 -0
- data/lib/active_record/encryption/key_provider.rb +46 -0
- data/lib/active_record/encryption/message.rb +33 -0
- data/lib/active_record/encryption/message_serializer.rb +92 -0
- data/lib/active_record/encryption/null_encryptor.rb +21 -0
- data/lib/active_record/encryption/properties.rb +76 -0
- data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
- data/lib/active_record/encryption/scheme.rb +96 -0
- data/lib/active_record/encryption.rb +56 -0
- data/lib/active_record/enum.rb +154 -63
- data/lib/active_record/errors.rb +171 -15
- data/lib/active_record/explain.rb +23 -3
- data/lib/active_record/explain_registry.rb +11 -6
- data/lib/active_record/explain_subscriber.rb +1 -1
- data/lib/active_record/fixture_set/file.rb +15 -1
- data/lib/active_record/fixture_set/model_metadata.rb +14 -4
- data/lib/active_record/fixture_set/render_context.rb +2 -0
- data/lib/active_record/fixture_set/table_row.rb +70 -14
- data/lib/active_record/fixture_set/table_rows.rb +4 -4
- data/lib/active_record/fixtures.rb +131 -86
- data/lib/active_record/future_result.rb +164 -0
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +81 -29
- data/lib/active_record/insert_all.rb +135 -22
- data/lib/active_record/integration.rb +11 -10
- data/lib/active_record/internal_metadata.rb +119 -33
- data/lib/active_record/legacy_yaml_adapter.rb +2 -39
- data/lib/active_record/locking/optimistic.rb +36 -21
- data/lib/active_record/locking/pessimistic.rb +15 -6
- data/lib/active_record/log_subscriber.rb +52 -19
- data/lib/active_record/marshalling.rb +56 -0
- data/lib/active_record/message_pack.rb +124 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
- data/lib/active_record/middleware/database_selector.rb +23 -13
- data/lib/active_record/middleware/shard_selector.rb +62 -0
- data/lib/active_record/migration/command_recorder.rb +112 -14
- data/lib/active_record/migration/compatibility.rb +221 -48
- data/lib/active_record/migration/default_strategy.rb +23 -0
- data/lib/active_record/migration/execution_strategy.rb +19 -0
- data/lib/active_record/migration/join_table.rb +1 -1
- data/lib/active_record/migration/pending_migration_connection.rb +21 -0
- data/lib/active_record/migration.rb +358 -171
- data/lib/active_record/model_schema.rb +120 -101
- data/lib/active_record/nested_attributes.rb +37 -18
- data/lib/active_record/no_touching.rb +3 -3
- data/lib/active_record/normalization.rb +167 -0
- data/lib/active_record/persistence.rb +405 -85
- data/lib/active_record/promise.rb +84 -0
- data/lib/active_record/query_cache.rb +3 -21
- data/lib/active_record/query_logs.rb +174 -0
- data/lib/active_record/query_logs_formatter.rb +41 -0
- data/lib/active_record/querying.rb +29 -6
- data/lib/active_record/railtie.rb +219 -43
- data/lib/active_record/railties/controller_runtime.rb +13 -9
- data/lib/active_record/railties/databases.rake +188 -252
- data/lib/active_record/railties/job_runtime.rb +23 -0
- data/lib/active_record/readonly_attributes.rb +41 -3
- data/lib/active_record/reflection.rb +241 -80
- data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
- data/lib/active_record/relation/batches.rb +192 -63
- data/lib/active_record/relation/calculations.rb +219 -90
- data/lib/active_record/relation/delegation.rb +27 -13
- data/lib/active_record/relation/finder_methods.rb +108 -51
- data/lib/active_record/relation/merger.rb +22 -13
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
- data/lib/active_record/relation/predicate_builder.rb +27 -20
- data/lib/active_record/relation/query_attribute.rb +30 -12
- data/lib/active_record/relation/query_methods.rb +654 -127
- data/lib/active_record/relation/record_fetch_warning.rb +7 -9
- data/lib/active_record/relation/spawn_methods.rb +20 -3
- data/lib/active_record/relation/where_clause.rb +10 -19
- data/lib/active_record/relation.rb +262 -120
- data/lib/active_record/result.rb +37 -11
- data/lib/active_record/runtime_registry.rb +18 -13
- data/lib/active_record/sanitization.rb +65 -20
- data/lib/active_record/schema.rb +36 -22
- data/lib/active_record/schema_dumper.rb +73 -24
- data/lib/active_record/schema_migration.rb +68 -33
- data/lib/active_record/scoping/default.rb +72 -15
- data/lib/active_record/scoping/named.rb +5 -13
- data/lib/active_record/scoping.rb +65 -34
- data/lib/active_record/secure_password.rb +60 -0
- data/lib/active_record/secure_token.rb +21 -3
- data/lib/active_record/serialization.rb +6 -1
- data/lib/active_record/signed_id.rb +10 -8
- data/lib/active_record/store.rb +16 -11
- data/lib/active_record/suppressor.rb +13 -15
- data/lib/active_record/table_metadata.rb +16 -3
- data/lib/active_record/tasks/database_tasks.rb +225 -136
- data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
- data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
- data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
- data/lib/active_record/test_databases.rb +1 -1
- data/lib/active_record/test_fixtures.rb +123 -99
- data/lib/active_record/timestamp.rb +29 -18
- data/lib/active_record/token_for.rb +113 -0
- data/lib/active_record/touch_later.rb +11 -6
- data/lib/active_record/transactions.rb +48 -27
- data/lib/active_record/translation.rb +3 -3
- data/lib/active_record/type/adapter_specific_registry.rb +32 -14
- data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
- data/lib/active_record/type/internal/timezone.rb +7 -2
- data/lib/active_record/type/serialized.rb +9 -5
- data/lib/active_record/type/time.rb +4 -0
- data/lib/active_record/type/type_map.rb +17 -20
- data/lib/active_record/type.rb +1 -2
- data/lib/active_record/validations/absence.rb +1 -1
- data/lib/active_record/validations/associated.rb +4 -4
- data/lib/active_record/validations/numericality.rb +5 -4
- data/lib/active_record/validations/presence.rb +5 -28
- data/lib/active_record/validations/uniqueness.rb +51 -6
- data/lib/active_record/validations.rb +8 -4
- data/lib/active_record/version.rb +1 -1
- data/lib/active_record.rb +335 -32
- data/lib/arel/attributes/attribute.rb +0 -8
- data/lib/arel/crud.rb +28 -22
- data/lib/arel/delete_manager.rb +18 -4
- data/lib/arel/errors.rb +10 -0
- data/lib/arel/factory_methods.rb +4 -0
- data/lib/arel/filter_predications.rb +9 -0
- data/lib/arel/insert_manager.rb +2 -3
- data/lib/arel/nodes/and.rb +4 -0
- data/lib/arel/nodes/binary.rb +6 -1
- data/lib/arel/nodes/bound_sql_literal.rb +61 -0
- data/lib/arel/nodes/casted.rb +1 -1
- data/lib/arel/nodes/cte.rb +36 -0
- data/lib/arel/nodes/delete_statement.rb +12 -13
- data/lib/arel/nodes/filter.rb +10 -0
- data/lib/arel/nodes/fragments.rb +35 -0
- data/lib/arel/nodes/function.rb +1 -0
- data/lib/arel/nodes/homogeneous_in.rb +1 -9
- data/lib/arel/nodes/insert_statement.rb +2 -2
- data/lib/arel/nodes/leading_join.rb +8 -0
- data/lib/arel/nodes/node.rb +111 -2
- data/lib/arel/nodes/select_core.rb +2 -2
- data/lib/arel/nodes/select_statement.rb +2 -2
- data/lib/arel/nodes/sql_literal.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +4 -0
- data/lib/arel/nodes/update_statement.rb +8 -3
- data/lib/arel/nodes.rb +5 -0
- data/lib/arel/predications.rb +13 -3
- data/lib/arel/select_manager.rb +10 -4
- data/lib/arel/table.rb +9 -6
- data/lib/arel/tree_manager.rb +0 -12
- data/lib/arel/update_manager.rb +18 -4
- data/lib/arel/visitors/dot.rb +80 -90
- data/lib/arel/visitors/mysql.rb +16 -3
- data/lib/arel/visitors/postgresql.rb +0 -10
- data/lib/arel/visitors/to_sql.rb +139 -19
- data/lib/arel/visitors/visitor.rb +2 -2
- data/lib/arel.rb +18 -3
- data/lib/rails/generators/active_record/application_record/USAGE +8 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
- data/lib/rails/generators/active_record/migration.rb +3 -1
- data/lib/rails/generators/active_record/model/USAGE +113 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
- data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
- data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
- data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
- metadata +93 -13
- data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
- data/lib/active_record/null_relation.rb +0 -67
@@ -27,14 +27,6 @@ module Arel # :nodoc: all
|
|
27
27
|
relation.able_to_type_cast?
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
31
|
-
class String < Attribute; end
|
32
|
-
class Time < Attribute; end
|
33
|
-
class Boolean < Attribute; end
|
34
|
-
class Decimal < Attribute; end
|
35
|
-
class Float < Attribute; end
|
36
|
-
class Integer < Attribute; end
|
37
|
-
class Undefined < Attribute; end
|
38
30
|
end
|
39
31
|
|
40
32
|
Attribute = Attributes::Attribute
|
data/lib/arel/crud.rb
CHANGED
@@ -4,23 +4,6 @@ module Arel # :nodoc: all
|
|
4
4
|
###
|
5
5
|
# FIXME hopefully we can remove this
|
6
6
|
module Crud
|
7
|
-
def compile_update(values, pk)
|
8
|
-
um = UpdateManager.new
|
9
|
-
|
10
|
-
if Nodes::SqlLiteral === values
|
11
|
-
relation = @ctx.from
|
12
|
-
else
|
13
|
-
relation = values.first.first.relation
|
14
|
-
end
|
15
|
-
um.key = pk
|
16
|
-
um.table relation
|
17
|
-
um.set values
|
18
|
-
um.take @ast.limit.expr if @ast.limit
|
19
|
-
um.order(*@ast.orders)
|
20
|
-
um.wheres = @ctx.wheres
|
21
|
-
um
|
22
|
-
end
|
23
|
-
|
24
7
|
def compile_insert(values)
|
25
8
|
im = create_insert
|
26
9
|
im.insert values
|
@@ -31,11 +14,34 @@ module Arel # :nodoc: all
|
|
31
14
|
InsertManager.new
|
32
15
|
end
|
33
16
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
17
|
+
def compile_update(
|
18
|
+
values,
|
19
|
+
key = nil,
|
20
|
+
having_clause = nil,
|
21
|
+
group_values_columns = []
|
22
|
+
)
|
23
|
+
um = UpdateManager.new(source)
|
24
|
+
um.set(values)
|
25
|
+
um.take(limit)
|
26
|
+
um.offset(offset)
|
27
|
+
um.order(*orders)
|
28
|
+
um.wheres = constraints
|
29
|
+
um.key = key
|
30
|
+
|
31
|
+
um.group(group_values_columns) unless group_values_columns.empty?
|
32
|
+
um.having(having_clause) unless having_clause.nil?
|
33
|
+
um
|
34
|
+
end
|
35
|
+
|
36
|
+
def compile_delete(key = nil, having_clause = nil, group_values_columns = [])
|
37
|
+
dm = DeleteManager.new(source)
|
38
|
+
dm.take(limit)
|
39
|
+
dm.offset(offset)
|
40
|
+
dm.order(*orders)
|
41
|
+
dm.wheres = constraints
|
42
|
+
dm.key = key
|
43
|
+
dm.group(group_values_columns) unless group_values_columns.empty?
|
44
|
+
dm.having(having_clause) unless having_clause.nil?
|
39
45
|
dm
|
40
46
|
end
|
41
47
|
end
|
data/lib/arel/delete_manager.rb
CHANGED
@@ -4,15 +4,29 @@ module Arel # :nodoc: all
|
|
4
4
|
class DeleteManager < Arel::TreeManager
|
5
5
|
include TreeManager::StatementMethods
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
|
9
|
-
@ast = Nodes::DeleteStatement.new
|
10
|
-
@ctx = @ast
|
7
|
+
def initialize(table = nil)
|
8
|
+
@ast = Nodes::DeleteStatement.new(table)
|
11
9
|
end
|
12
10
|
|
13
11
|
def from(relation)
|
14
12
|
@ast.relation = relation
|
15
13
|
self
|
16
14
|
end
|
15
|
+
|
16
|
+
def group(columns)
|
17
|
+
columns.each do |column|
|
18
|
+
column = Nodes::SqlLiteral.new(column) if String === column
|
19
|
+
column = Nodes::SqlLiteral.new(column.to_s) if Symbol === column
|
20
|
+
|
21
|
+
@ast.groups.push Nodes::Group.new column
|
22
|
+
end
|
23
|
+
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def having(expr)
|
28
|
+
@ast.havings << expr
|
29
|
+
self
|
30
|
+
end
|
17
31
|
end
|
18
32
|
end
|
data/lib/arel/errors.rb
CHANGED
data/lib/arel/factory_methods.rb
CHANGED
data/lib/arel/insert_manager.rb
CHANGED
data/lib/arel/nodes/and.rb
CHANGED
data/lib/arel/nodes/binary.rb
CHANGED
@@ -39,6 +39,12 @@ module Arel # :nodoc: all
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
class As < Binary
|
43
|
+
def to_cte
|
44
|
+
Arel::Nodes::Cte.new(left.name, right)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
42
48
|
class Between < Binary; include FetchAttribute; end
|
43
49
|
|
44
50
|
class GreaterThan < Binary
|
@@ -112,7 +118,6 @@ module Arel # :nodoc: all
|
|
112
118
|
end
|
113
119
|
|
114
120
|
%w{
|
115
|
-
As
|
116
121
|
Assignment
|
117
122
|
Join
|
118
123
|
Union
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arel # :nodoc: all
|
4
|
+
module Nodes
|
5
|
+
class BoundSqlLiteral < NodeExpression
|
6
|
+
attr_reader :sql_with_placeholders, :positional_binds, :named_binds
|
7
|
+
|
8
|
+
def initialize(sql_with_placeholders, positional_binds, named_binds)
|
9
|
+
if !positional_binds.empty? && !named_binds.empty?
|
10
|
+
raise BindError.new("cannot mix positional and named binds", sql_with_placeholders)
|
11
|
+
elsif !positional_binds.empty?
|
12
|
+
if positional_binds.size != (expected = sql_with_placeholders.count("?"))
|
13
|
+
raise BindError.new("wrong number of bind variables (#{positional_binds.size} for #{expected})", sql_with_placeholders)
|
14
|
+
end
|
15
|
+
elsif !named_binds.empty?
|
16
|
+
tokens_in_string = sql_with_placeholders.scan(/:(?<!::)([a-zA-Z]\w*)/).flatten.map(&:to_sym).uniq
|
17
|
+
tokens_in_hash = named_binds.keys.map(&:to_sym).uniq
|
18
|
+
|
19
|
+
if !(missing = (tokens_in_string - tokens_in_hash)).empty?
|
20
|
+
if missing.size == 1
|
21
|
+
raise BindError.new("missing value for #{missing.first.inspect}", sql_with_placeholders)
|
22
|
+
else
|
23
|
+
raise BindError.new("missing values for #{missing.inspect}", sql_with_placeholders)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
@sql_with_placeholders = sql_with_placeholders
|
29
|
+
if !positional_binds.empty?
|
30
|
+
@positional_binds = positional_binds
|
31
|
+
@named_binds = nil
|
32
|
+
else
|
33
|
+
@positional_binds = nil
|
34
|
+
@named_binds = named_binds
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def hash
|
39
|
+
[self.class, sql_with_placeholders, positional_binds, named_binds].hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def eql?(other)
|
43
|
+
self.class == other.class &&
|
44
|
+
sql_with_placeholders == other.sql_with_placeholders &&
|
45
|
+
positional_binds == other.positional_binds &&
|
46
|
+
named_binds == other.named_binds
|
47
|
+
end
|
48
|
+
alias :== :eql?
|
49
|
+
|
50
|
+
def +(other)
|
51
|
+
raise ArgumentError, "Expected Arel node" unless Arel.arel_node?(other)
|
52
|
+
|
53
|
+
Fragments.new([self, other])
|
54
|
+
end
|
55
|
+
|
56
|
+
def inspect
|
57
|
+
"#<#{self.class.name} #{sql_with_placeholders.inspect} #{(named_binds || positional_binds).inspect}>"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/arel/nodes/casted.rb
CHANGED
@@ -47,7 +47,7 @@ module Arel # :nodoc: all
|
|
47
47
|
|
48
48
|
def self.build_quoted(other, attribute = nil)
|
49
49
|
case other
|
50
|
-
when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::
|
50
|
+
when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::SelectManager, Arel::Nodes::SqlLiteral, ActiveModel::Attribute
|
51
51
|
other
|
52
52
|
else
|
53
53
|
case attribute
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arel # :nodoc: all
|
4
|
+
module Nodes
|
5
|
+
class Cte < Arel::Nodes::Binary
|
6
|
+
alias :name :left
|
7
|
+
alias :relation :right
|
8
|
+
attr_reader :materialized
|
9
|
+
|
10
|
+
def initialize(name, relation, materialized: nil)
|
11
|
+
super(name, relation)
|
12
|
+
@materialized = materialized
|
13
|
+
end
|
14
|
+
|
15
|
+
def hash
|
16
|
+
[name, relation, materialized].hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def eql?(other)
|
20
|
+
self.class == other.class &&
|
21
|
+
self.name == other.name &&
|
22
|
+
self.relation == other.relation &&
|
23
|
+
self.materialized == other.materialized
|
24
|
+
end
|
25
|
+
alias :== :eql?
|
26
|
+
|
27
|
+
def to_cte
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_table
|
32
|
+
Arel::Table.new(name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -3,17 +3,14 @@
|
|
3
3
|
module Arel # :nodoc: all
|
4
4
|
module Nodes
|
5
5
|
class DeleteStatement < Arel::Nodes::Node
|
6
|
-
attr_accessor :
|
7
|
-
|
8
|
-
alias :relation :left
|
9
|
-
alias :relation= :left=
|
10
|
-
alias :wheres :right
|
11
|
-
alias :wheres= :right=
|
6
|
+
attr_accessor :relation, :wheres, :groups, :havings, :orders, :limit, :offset, :key
|
12
7
|
|
13
8
|
def initialize(relation = nil, wheres = [])
|
14
9
|
super()
|
15
|
-
@
|
16
|
-
@
|
10
|
+
@relation = relation
|
11
|
+
@wheres = wheres
|
12
|
+
@groups = []
|
13
|
+
@havings = []
|
17
14
|
@orders = []
|
18
15
|
@limit = nil
|
19
16
|
@offset = nil
|
@@ -22,19 +19,21 @@ module Arel # :nodoc: all
|
|
22
19
|
|
23
20
|
def initialize_copy(other)
|
24
21
|
super
|
25
|
-
@
|
26
|
-
@
|
22
|
+
@relation = @relation.clone if @relation
|
23
|
+
@wheres = @wheres.clone if @wheres
|
27
24
|
end
|
28
25
|
|
29
26
|
def hash
|
30
|
-
[self.class, @
|
27
|
+
[self.class, @relation, @wheres, @orders, @limit, @offset, @key].hash
|
31
28
|
end
|
32
29
|
|
33
30
|
def eql?(other)
|
34
31
|
self.class == other.class &&
|
35
|
-
self.
|
36
|
-
self.
|
32
|
+
self.relation == other.relation &&
|
33
|
+
self.wheres == other.wheres &&
|
37
34
|
self.orders == other.orders &&
|
35
|
+
self.groups == other.groups &&
|
36
|
+
self.havings == other.havings &&
|
38
37
|
self.limit == other.limit &&
|
39
38
|
self.offset == other.offset &&
|
40
39
|
self.key == other.key
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arel # :nodoc: all
|
4
|
+
module Nodes
|
5
|
+
class Fragments < Arel::Nodes::Node
|
6
|
+
attr_reader :values
|
7
|
+
|
8
|
+
def initialize(values = [])
|
9
|
+
super()
|
10
|
+
@values = values
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize_copy(other)
|
14
|
+
super
|
15
|
+
@values = @values.clone
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash
|
19
|
+
[@values].hash
|
20
|
+
end
|
21
|
+
|
22
|
+
def +(other)
|
23
|
+
raise ArgumentError, "Expected Arel node" unless Arel.arel_node?(other)
|
24
|
+
|
25
|
+
self.class.new([*@values, other])
|
26
|
+
end
|
27
|
+
|
28
|
+
def eql?(other)
|
29
|
+
self.class == other.class &&
|
30
|
+
self.values == other.values
|
31
|
+
end
|
32
|
+
alias :== :eql?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/arel/nodes/function.rb
CHANGED
@@ -36,14 +36,6 @@ module Arel # :nodoc: all
|
|
36
36
|
attribute.quoted_array(values)
|
37
37
|
end
|
38
38
|
|
39
|
-
def table_name
|
40
|
-
attribute.relation.table_alias || attribute.relation.name
|
41
|
-
end
|
42
|
-
|
43
|
-
def column_name
|
44
|
-
attribute.name
|
45
|
-
end
|
46
|
-
|
47
39
|
def casted_values
|
48
40
|
type = attribute.type_caster
|
49
41
|
|
@@ -56,7 +48,7 @@ module Arel # :nodoc: all
|
|
56
48
|
end
|
57
49
|
|
58
50
|
def proc_for_binds
|
59
|
-
-> value { ActiveModel::Attribute.with_cast_value(attribute.name, value,
|
51
|
+
-> value { ActiveModel::Attribute.with_cast_value(attribute.name, value, ActiveModel::Type.default_value) }
|
60
52
|
end
|
61
53
|
|
62
54
|
def fetch_attribute(&block)
|
@@ -5,9 +5,9 @@ module Arel # :nodoc: all
|
|
5
5
|
class InsertStatement < Arel::Nodes::Node
|
6
6
|
attr_accessor :relation, :columns, :values, :select
|
7
7
|
|
8
|
-
def initialize
|
8
|
+
def initialize(relation = nil)
|
9
9
|
super()
|
10
|
-
@relation =
|
10
|
+
@relation = relation
|
11
11
|
@columns = []
|
12
12
|
@values = nil
|
13
13
|
@select = nil
|
data/lib/arel/nodes/node.rb
CHANGED
@@ -2,8 +2,117 @@
|
|
2
2
|
|
3
3
|
module Arel # :nodoc: all
|
4
4
|
module Nodes
|
5
|
-
|
6
|
-
#
|
5
|
+
# = Using +Arel::Nodes::Node+
|
6
|
+
#
|
7
|
+
# Active Record uses Arel to compose SQL statements. Instead of building SQL strings directly, it's building an
|
8
|
+
# abstract syntax tree (AST) of the statement using various types of Arel::Nodes::Node. Each node represents a
|
9
|
+
# fragment of a SQL statement.
|
10
|
+
#
|
11
|
+
# The intermediate representation allows Arel to compile the statement into the database's specific SQL dialect
|
12
|
+
# only before sending it without having to care about the nuances of each database when building the statement.
|
13
|
+
# It also allows easier composition of statements without having to resort to (brittle and unsafe) string manipulation.
|
14
|
+
#
|
15
|
+
# == Building constraints
|
16
|
+
#
|
17
|
+
# One of the most common use cases of Arel is generating constraints for +SELECT+ statements. To help with that,
|
18
|
+
# most nodes include a couple of useful factory methods to create subtree structures for common constraints. For
|
19
|
+
# a full list of those, please refer to Arel::Predications.
|
20
|
+
#
|
21
|
+
# The following example creates an equality constraint where the value of the name column on the users table
|
22
|
+
# matches the value DHH.
|
23
|
+
#
|
24
|
+
# users = Arel::Table.new(:users)
|
25
|
+
# constraint = users[:name].eq("DHH")
|
26
|
+
#
|
27
|
+
# # => Arel::Nodes::Equality.new(
|
28
|
+
# # Arel::Attributes::Attribute.new(users, "name"),
|
29
|
+
# # Arel::Nodes::Casted.new(
|
30
|
+
# # "DHH",
|
31
|
+
# # Arel::Attributes::Attribute.new(users, "name")
|
32
|
+
# # )
|
33
|
+
# # )
|
34
|
+
#
|
35
|
+
# The resulting SQL fragment will look like this:
|
36
|
+
#
|
37
|
+
# "users"."name" = 'DHH'
|
38
|
+
#
|
39
|
+
# The constraint fragments can be used with regular ActiveRecord::Relation objects instead of a Hash. The
|
40
|
+
# following two examples show two ways of creating the same query.
|
41
|
+
#
|
42
|
+
# User.where(name: 'DHH')
|
43
|
+
#
|
44
|
+
# # SELECT "users".* FROM "users" WHERE "users"."name" = 'DHH'
|
45
|
+
#
|
46
|
+
# users = User.arel_table
|
47
|
+
#
|
48
|
+
# User.where(users[:name].eq('DHH'))
|
49
|
+
#
|
50
|
+
# # SELECT "users".* FROM "users" WHERE "users"."name" = 'DHH'
|
51
|
+
#
|
52
|
+
# == Functions
|
53
|
+
#
|
54
|
+
# Arel comes with built-in support for SQL functions like +COUNT+, +SUM+, +MIN+, +MAX+, and +AVG+. The
|
55
|
+
# Arel::Expressions module includes factory methods for the default functions.
|
56
|
+
#
|
57
|
+
# employees = Employee.arel_table
|
58
|
+
#
|
59
|
+
# Employee.select(employees[:department_id], employees[:salary].average).group(employees[:department_id])
|
60
|
+
#
|
61
|
+
# # SELECT "employees"."department_id", AVG("employees"."salary")
|
62
|
+
# # FROM "employees" GROUP BY "employees"."department_id"
|
63
|
+
#
|
64
|
+
# It’s also possible to use custom functions by using the Arel::Nodes::NamedFunction node type. It accepts a
|
65
|
+
# function name and an array of parameters.
|
66
|
+
#
|
67
|
+
# Arel::Nodes::NamedFunction.new('date_trunc', [Arel::Nodes.build_quoted('day'), User.arel_table[:created_at]])
|
68
|
+
#
|
69
|
+
# # date_trunc('day', "users"."created_at")
|
70
|
+
#
|
71
|
+
# == Quoting & bind params
|
72
|
+
#
|
73
|
+
# Values that you pass to Arel nodes need to be quoted or wrapped in bind params. This ensures they are properly
|
74
|
+
# converted into the correct format without introducing a possible SQL injection vulnerability. Most factory
|
75
|
+
# methods (like +eq+, +gt+, +lteq+, …) quote passed values automatically. When not using a factory method, it’s
|
76
|
+
# possible to convert a value and wrap it in an Arel::Nodes::Quoted node (if necessary) by calling +Arel::Nodes.
|
77
|
+
# build_quoted+.
|
78
|
+
#
|
79
|
+
# Arel::Nodes.build_quoted("foo") # 'foo'
|
80
|
+
# Arel::Nodes.build_quoted(12.3) # 12.3
|
81
|
+
#
|
82
|
+
# Instead of quoting values and embedding them directly in the SQL statement, it’s also possible to create bind
|
83
|
+
# params. This keeps the actual values outside of the statement and allows using the prepared statement feature
|
84
|
+
# of some databases.
|
85
|
+
#
|
86
|
+
# attribute = ActiveRecord::Relation::QueryAttribute.new(:name, "DHH", ActiveRecord::Type::String.new)
|
87
|
+
# Arel::Nodes::BindParam.new(attribute)
|
88
|
+
#
|
89
|
+
# When ActiveRecord runs the query, bind params are replaced by placeholders (like +$1+) and the values are passed
|
90
|
+
# separately.
|
91
|
+
#
|
92
|
+
# == SQL Literals
|
93
|
+
#
|
94
|
+
# For cases where there is no way to represent a particular SQL fragment using Arel nodes, you can use an SQL
|
95
|
+
# literal. SQL literals are strings that Arel will treat “as is”.
|
96
|
+
#
|
97
|
+
# Arel.sql('LOWER("users"."name")').eq('dhh')
|
98
|
+
#
|
99
|
+
# # LOWER("users"."name") = 'dhh'
|
100
|
+
#
|
101
|
+
# Please keep in mind that passing data as raw SQL literals might introduce a possible SQL injection. However,
|
102
|
+
# `Arel.sql` supports binding parameters which will ensure proper quoting. This can be useful when you need to
|
103
|
+
# control the exact SQL you run, but you still have potentially user-supplied values.
|
104
|
+
#
|
105
|
+
# Arel.sql('LOWER("users"."name") = ?', 'dhh')
|
106
|
+
#
|
107
|
+
# # LOWER("users"."name") = 'dhh'
|
108
|
+
#
|
109
|
+
# You can also combine SQL literals.
|
110
|
+
#
|
111
|
+
# sql = Arel.sql('SELECT * FROM "users" WHERE ')
|
112
|
+
# sql += Arel.sql('LOWER("users"."name") = :name', name: 'dhh')
|
113
|
+
# sql += Arel.sql('AND "users"."age" > :age', age: 35)
|
114
|
+
#
|
115
|
+
# # SELECT * FROM "users" WHERE LOWER("users"."name") = 'dhh' AND "users"."age" > '35'
|
7
116
|
class Node
|
8
117
|
include Arel::FactoryMethods
|
9
118
|
|
@@ -6,9 +6,9 @@ module Arel # :nodoc: all
|
|
6
6
|
attr_accessor :projections, :wheres, :groups, :windows, :comment
|
7
7
|
attr_accessor :havings, :source, :set_quantifier, :optimizer_hints
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(relation = nil)
|
10
10
|
super()
|
11
|
-
@source = JoinSource.new
|
11
|
+
@source = JoinSource.new(relation)
|
12
12
|
|
13
13
|
# https://ronsavage.github.io/SQL/sql-92.bnf.html#set%20quantifier
|
14
14
|
@set_quantifier = nil
|
@@ -6,9 +6,9 @@ module Arel # :nodoc: all
|
|
6
6
|
attr_reader :cores
|
7
7
|
attr_accessor :limit, :orders, :lock, :offset, :with
|
8
8
|
|
9
|
-
def initialize(
|
9
|
+
def initialize(relation = nil)
|
10
10
|
super()
|
11
|
-
@cores =
|
11
|
+
@cores = [SelectCore.new(relation)]
|
12
12
|
@orders = []
|
13
13
|
@limit = nil
|
14
14
|
@lock = nil
|
@@ -3,12 +3,15 @@
|
|
3
3
|
module Arel # :nodoc: all
|
4
4
|
module Nodes
|
5
5
|
class UpdateStatement < Arel::Nodes::Node
|
6
|
-
attr_accessor :relation, :wheres, :values, :orders, :limit, :offset, :key
|
6
|
+
attr_accessor :relation, :wheres, :values, :groups, :havings, :orders, :limit, :offset, :key
|
7
7
|
|
8
|
-
def initialize
|
9
|
-
|
8
|
+
def initialize(relation = nil)
|
9
|
+
super()
|
10
|
+
@relation = relation
|
10
11
|
@wheres = []
|
11
12
|
@values = []
|
13
|
+
@groups = []
|
14
|
+
@havings = []
|
12
15
|
@orders = []
|
13
16
|
@limit = nil
|
14
17
|
@offset = nil
|
@@ -30,6 +33,8 @@ module Arel # :nodoc: all
|
|
30
33
|
self.relation == other.relation &&
|
31
34
|
self.wheres == other.wheres &&
|
32
35
|
self.values == other.values &&
|
36
|
+
self.groups == other.groups &&
|
37
|
+
self.havings == other.havings &&
|
33
38
|
self.orders == other.orders &&
|
34
39
|
self.limit == other.limit &&
|
35
40
|
self.offset == other.offset &&
|