activerecord 6.1.7 → 7.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (311) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2030 -1020
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +18 -18
  5. data/lib/active_record/aggregations.rb +17 -14
  6. data/lib/active_record/association_relation.rb +1 -11
  7. data/lib/active_record/associations/association.rb +51 -19
  8. data/lib/active_record/associations/association_scope.rb +17 -12
  9. data/lib/active_record/associations/belongs_to_association.rb +28 -9
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
  11. data/lib/active_record/associations/builder/association.rb +11 -5
  12. data/lib/active_record/associations/builder/belongs_to.rb +40 -14
  13. data/lib/active_record/associations/builder/collection_association.rb +10 -3
  14. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  15. data/lib/active_record/associations/builder/has_many.rb +3 -2
  16. data/lib/active_record/associations/builder/has_one.rb +2 -1
  17. data/lib/active_record/associations/builder/singular_association.rb +6 -2
  18. data/lib/active_record/associations/collection_association.rb +39 -35
  19. data/lib/active_record/associations/collection_proxy.rb +30 -15
  20. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  21. data/lib/active_record/associations/foreign_association.rb +10 -3
  22. data/lib/active_record/associations/has_many_association.rb +28 -18
  23. data/lib/active_record/associations/has_many_through_association.rb +12 -7
  24. data/lib/active_record/associations/has_one_association.rb +20 -10
  25. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  26. data/lib/active_record/associations/join_dependency/join_association.rb +3 -2
  27. data/lib/active_record/associations/join_dependency.rb +28 -20
  28. data/lib/active_record/associations/preloader/association.rb +210 -52
  29. data/lib/active_record/associations/preloader/batch.rb +48 -0
  30. data/lib/active_record/associations/preloader/branch.rb +147 -0
  31. data/lib/active_record/associations/preloader/through_association.rb +50 -14
  32. data/lib/active_record/associations/preloader.rb +50 -121
  33. data/lib/active_record/associations/singular_association.rb +9 -3
  34. data/lib/active_record/associations/through_association.rb +25 -14
  35. data/lib/active_record/associations.rb +446 -306
  36. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  37. data/lib/active_record/attribute_assignment.rb +1 -3
  38. data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
  39. data/lib/active_record/attribute_methods/dirty.rb +73 -22
  40. data/lib/active_record/attribute_methods/primary_key.rb +78 -26
  41. data/lib/active_record/attribute_methods/query.rb +31 -19
  42. data/lib/active_record/attribute_methods/read.rb +27 -12
  43. data/lib/active_record/attribute_methods/serialization.rb +194 -37
  44. data/lib/active_record/attribute_methods/time_zone_conversion.rb +8 -3
  45. data/lib/active_record/attribute_methods/write.rb +12 -15
  46. data/lib/active_record/attribute_methods.rb +161 -40
  47. data/lib/active_record/attributes.rb +27 -38
  48. data/lib/active_record/autosave_association.rb +65 -31
  49. data/lib/active_record/base.rb +25 -2
  50. data/lib/active_record/callbacks.rb +18 -34
  51. data/lib/active_record/coders/column_serializer.rb +61 -0
  52. data/lib/active_record/coders/json.rb +1 -1
  53. data/lib/active_record/coders/yaml_column.rb +70 -46
  54. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
  55. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
  56. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +113 -597
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +78 -27
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +367 -141
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -150
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +317 -164
  70. data/lib/active_record/connection_adapters/column.rb +13 -0
  71. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  72. data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
  73. data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
  74. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  75. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
  76. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
  77. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +39 -14
  78. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
  79. data/lib/active_record/connection_adapters/mysql2_adapter.rb +112 -55
  80. data/lib/active_record/connection_adapters/pool_config.rb +20 -11
  81. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
  84. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  85. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +6 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
  89. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  90. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
  91. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  94. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
  96. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
  97. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
  98. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
  99. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
  100. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +397 -75
  101. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
  102. data/lib/active_record/connection_adapters/postgresql_adapter.rb +508 -246
  103. data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
  104. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  105. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
  106. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
  107. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
  108. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
  109. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +296 -104
  110. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  111. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  112. data/lib/active_record/connection_adapters/trilogy_adapter.rb +258 -0
  113. data/lib/active_record/connection_adapters.rb +9 -6
  114. data/lib/active_record/connection_handling.rb +108 -137
  115. data/lib/active_record/core.rb +242 -233
  116. data/lib/active_record/counter_cache.rb +52 -27
  117. data/lib/active_record/database_configurations/connection_url_resolver.rb +3 -2
  118. data/lib/active_record/database_configurations/database_config.rb +21 -12
  119. data/lib/active_record/database_configurations/hash_config.rb +88 -16
  120. data/lib/active_record/database_configurations/url_config.rb +18 -12
  121. data/lib/active_record/database_configurations.rb +95 -59
  122. data/lib/active_record/delegated_type.rb +66 -20
  123. data/lib/active_record/deprecator.rb +7 -0
  124. data/lib/active_record/destroy_association_async_job.rb +4 -2
  125. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  126. data/lib/active_record/dynamic_matchers.rb +1 -1
  127. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  128. data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
  129. data/lib/active_record/encryption/cipher.rb +53 -0
  130. data/lib/active_record/encryption/config.rb +68 -0
  131. data/lib/active_record/encryption/configurable.rb +60 -0
  132. data/lib/active_record/encryption/context.rb +42 -0
  133. data/lib/active_record/encryption/contexts.rb +76 -0
  134. data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
  135. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  136. data/lib/active_record/encryption/encryptable_record.rb +230 -0
  137. data/lib/active_record/encryption/encrypted_attribute_type.rb +155 -0
  138. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  139. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  140. data/lib/active_record/encryption/encryptor.rb +155 -0
  141. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  142. data/lib/active_record/encryption/errors.rb +15 -0
  143. data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
  144. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  145. data/lib/active_record/encryption/key.rb +28 -0
  146. data/lib/active_record/encryption/key_generator.rb +53 -0
  147. data/lib/active_record/encryption/key_provider.rb +46 -0
  148. data/lib/active_record/encryption/message.rb +33 -0
  149. data/lib/active_record/encryption/message_serializer.rb +92 -0
  150. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  151. data/lib/active_record/encryption/properties.rb +76 -0
  152. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  153. data/lib/active_record/encryption/scheme.rb +100 -0
  154. data/lib/active_record/encryption.rb +58 -0
  155. data/lib/active_record/enum.rb +154 -63
  156. data/lib/active_record/errors.rb +172 -15
  157. data/lib/active_record/explain.rb +23 -3
  158. data/lib/active_record/explain_registry.rb +11 -6
  159. data/lib/active_record/explain_subscriber.rb +1 -1
  160. data/lib/active_record/fixture_set/file.rb +15 -1
  161. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  162. data/lib/active_record/fixture_set/render_context.rb +2 -0
  163. data/lib/active_record/fixture_set/table_row.rb +70 -14
  164. data/lib/active_record/fixture_set/table_rows.rb +4 -4
  165. data/lib/active_record/fixtures.rb +147 -86
  166. data/lib/active_record/future_result.rb +174 -0
  167. data/lib/active_record/gem_version.rb +3 -3
  168. data/lib/active_record/inheritance.rb +81 -29
  169. data/lib/active_record/insert_all.rb +135 -22
  170. data/lib/active_record/integration.rb +11 -10
  171. data/lib/active_record/internal_metadata.rb +119 -33
  172. data/lib/active_record/legacy_yaml_adapter.rb +2 -39
  173. data/lib/active_record/locking/optimistic.rb +37 -22
  174. data/lib/active_record/locking/pessimistic.rb +15 -6
  175. data/lib/active_record/log_subscriber.rb +52 -19
  176. data/lib/active_record/marshalling.rb +59 -0
  177. data/lib/active_record/message_pack.rb +124 -0
  178. data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
  179. data/lib/active_record/middleware/database_selector.rb +23 -13
  180. data/lib/active_record/middleware/shard_selector.rb +62 -0
  181. data/lib/active_record/migration/command_recorder.rb +112 -14
  182. data/lib/active_record/migration/compatibility.rb +233 -46
  183. data/lib/active_record/migration/default_strategy.rb +23 -0
  184. data/lib/active_record/migration/execution_strategy.rb +19 -0
  185. data/lib/active_record/migration/join_table.rb +1 -1
  186. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  187. data/lib/active_record/migration.rb +361 -173
  188. data/lib/active_record/model_schema.rb +125 -101
  189. data/lib/active_record/nested_attributes.rb +50 -20
  190. data/lib/active_record/no_touching.rb +3 -3
  191. data/lib/active_record/normalization.rb +167 -0
  192. data/lib/active_record/persistence.rb +409 -88
  193. data/lib/active_record/promise.rb +84 -0
  194. data/lib/active_record/query_cache.rb +4 -22
  195. data/lib/active_record/query_logs.rb +174 -0
  196. data/lib/active_record/query_logs_formatter.rb +41 -0
  197. data/lib/active_record/querying.rb +29 -6
  198. data/lib/active_record/railtie.rb +220 -44
  199. data/lib/active_record/railties/controller_runtime.rb +15 -10
  200. data/lib/active_record/railties/databases.rake +188 -252
  201. data/lib/active_record/railties/job_runtime.rb +23 -0
  202. data/lib/active_record/readonly_attributes.rb +41 -3
  203. data/lib/active_record/reflection.rb +248 -81
  204. data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
  205. data/lib/active_record/relation/batches.rb +192 -63
  206. data/lib/active_record/relation/calculations.rb +246 -90
  207. data/lib/active_record/relation/delegation.rb +28 -14
  208. data/lib/active_record/relation/finder_methods.rb +108 -51
  209. data/lib/active_record/relation/merger.rb +22 -13
  210. data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
  211. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +10 -7
  212. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  213. data/lib/active_record/relation/predicate_builder.rb +27 -20
  214. data/lib/active_record/relation/query_attribute.rb +30 -12
  215. data/lib/active_record/relation/query_methods.rb +670 -129
  216. data/lib/active_record/relation/record_fetch_warning.rb +7 -9
  217. data/lib/active_record/relation/spawn_methods.rb +20 -3
  218. data/lib/active_record/relation/where_clause.rb +10 -19
  219. data/lib/active_record/relation.rb +287 -120
  220. data/lib/active_record/result.rb +37 -11
  221. data/lib/active_record/runtime_registry.rb +32 -13
  222. data/lib/active_record/sanitization.rb +65 -20
  223. data/lib/active_record/schema.rb +36 -22
  224. data/lib/active_record/schema_dumper.rb +73 -24
  225. data/lib/active_record/schema_migration.rb +68 -33
  226. data/lib/active_record/scoping/default.rb +72 -15
  227. data/lib/active_record/scoping/named.rb +5 -13
  228. data/lib/active_record/scoping.rb +65 -34
  229. data/lib/active_record/secure_password.rb +60 -0
  230. data/lib/active_record/secure_token.rb +21 -3
  231. data/lib/active_record/serialization.rb +6 -1
  232. data/lib/active_record/signed_id.rb +10 -8
  233. data/lib/active_record/store.rb +10 -10
  234. data/lib/active_record/suppressor.rb +13 -15
  235. data/lib/active_record/table_metadata.rb +16 -3
  236. data/lib/active_record/tasks/database_tasks.rb +251 -140
  237. data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
  238. data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
  239. data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
  240. data/lib/active_record/test_databases.rb +1 -1
  241. data/lib/active_record/test_fixtures.rb +117 -96
  242. data/lib/active_record/timestamp.rb +32 -19
  243. data/lib/active_record/token_for.rb +113 -0
  244. data/lib/active_record/touch_later.rb +11 -6
  245. data/lib/active_record/transactions.rb +48 -27
  246. data/lib/active_record/translation.rb +3 -3
  247. data/lib/active_record/type/adapter_specific_registry.rb +32 -14
  248. data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
  249. data/lib/active_record/type/internal/timezone.rb +7 -2
  250. data/lib/active_record/type/serialized.rb +9 -5
  251. data/lib/active_record/type/time.rb +4 -0
  252. data/lib/active_record/type/type_map.rb +17 -20
  253. data/lib/active_record/type.rb +1 -2
  254. data/lib/active_record/validations/absence.rb +1 -1
  255. data/lib/active_record/validations/associated.rb +4 -4
  256. data/lib/active_record/validations/numericality.rb +5 -4
  257. data/lib/active_record/validations/presence.rb +5 -28
  258. data/lib/active_record/validations/uniqueness.rb +51 -6
  259. data/lib/active_record/validations.rb +8 -4
  260. data/lib/active_record/version.rb +1 -1
  261. data/lib/active_record.rb +335 -32
  262. data/lib/arel/attributes/attribute.rb +0 -8
  263. data/lib/arel/crud.rb +28 -22
  264. data/lib/arel/delete_manager.rb +18 -4
  265. data/lib/arel/errors.rb +10 -0
  266. data/lib/arel/factory_methods.rb +4 -0
  267. data/lib/arel/filter_predications.rb +9 -0
  268. data/lib/arel/insert_manager.rb +2 -3
  269. data/lib/arel/nodes/and.rb +4 -0
  270. data/lib/arel/nodes/binary.rb +6 -1
  271. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  272. data/lib/arel/nodes/casted.rb +1 -1
  273. data/lib/arel/nodes/cte.rb +36 -0
  274. data/lib/arel/nodes/delete_statement.rb +12 -13
  275. data/lib/arel/nodes/filter.rb +10 -0
  276. data/lib/arel/nodes/fragments.rb +35 -0
  277. data/lib/arel/nodes/function.rb +1 -0
  278. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  279. data/lib/arel/nodes/insert_statement.rb +2 -2
  280. data/lib/arel/nodes/leading_join.rb +8 -0
  281. data/lib/arel/nodes/node.rb +111 -2
  282. data/lib/arel/nodes/select_core.rb +2 -2
  283. data/lib/arel/nodes/select_statement.rb +2 -2
  284. data/lib/arel/nodes/sql_literal.rb +6 -0
  285. data/lib/arel/nodes/table_alias.rb +4 -0
  286. data/lib/arel/nodes/update_statement.rb +8 -3
  287. data/lib/arel/nodes.rb +5 -0
  288. data/lib/arel/predications.rb +13 -3
  289. data/lib/arel/select_manager.rb +10 -4
  290. data/lib/arel/table.rb +9 -6
  291. data/lib/arel/tree_manager.rb +5 -13
  292. data/lib/arel/update_manager.rb +18 -4
  293. data/lib/arel/visitors/dot.rb +80 -90
  294. data/lib/arel/visitors/mysql.rb +16 -3
  295. data/lib/arel/visitors/postgresql.rb +0 -10
  296. data/lib/arel/visitors/to_sql.rb +141 -20
  297. data/lib/arel/visitors/visitor.rb +2 -2
  298. data/lib/arel.rb +18 -3
  299. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  300. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  301. data/lib/rails/generators/active_record/migration.rb +3 -1
  302. data/lib/rails/generators/active_record/model/USAGE +113 -0
  303. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  304. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
  305. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  306. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  307. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  308. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  309. metadata +96 -16
  310. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  311. data/lib/active_record/null_relation.rb +0 -67
@@ -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
@@ -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::Nodes::BindParam, Arel::SelectManager, Arel::Nodes::Quoted, Arel::Nodes::SqlLiteral
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 :left, :right, :orders, :limit, :offset, :key
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
- @left = relation
16
- @right = wheres
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
- @left = @left.clone if @left
26
- @right = @right.clone if @right
22
+ @relation = @relation.clone if @relation
23
+ @wheres = @wheres.clone if @wheres
27
24
  end
28
25
 
29
26
  def hash
30
- [self.class, @left, @right, @orders, @limit, @offset, @key].hash
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.left == other.left &&
36
- self.right == other.right &&
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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arel # :nodoc: all
4
+ module Nodes
5
+ class Filter < Binary
6
+ include Arel::WindowPredications
7
+ include Arel::AliasPredication
8
+ end
9
+ end
10
+ end
@@ -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
@@ -4,6 +4,7 @@ module Arel # :nodoc: all
4
4
  module Nodes
5
5
  class Function < Arel::Nodes::NodeExpression
6
6
  include Arel::WindowPredications
7
+ include Arel::FilterPredications
7
8
  attr_accessor :expressions, :alias, :distinct
8
9
 
9
10
  def initialize(expr, aliaz = nil)
@@ -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, attribute.type_caster) }
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 = nil
10
+ @relation = relation
11
11
  @columns = []
12
12
  @values = nil
13
13
  @select = nil
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Arel # :nodoc: all
4
+ module Nodes
5
+ class LeadingJoin < Arel::Nodes::InnerJoin
6
+ end
7
+ end
8
+ end
@@ -2,8 +2,117 @@
2
2
 
3
3
  module Arel # :nodoc: all
4
4
  module Nodes
5
- ###
6
- # Abstract base class for all AST nodes
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 nil
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(cores = [SelectCore.new])
9
+ def initialize(relation = nil)
10
10
  super()
11
- @cores = cores
11
+ @cores = [SelectCore.new(relation)]
12
12
  @orders = []
13
13
  @limit = nil
14
14
  @lock = nil
@@ -14,6 +14,12 @@ module Arel # :nodoc: all
14
14
 
15
15
  def fetch_attribute
16
16
  end
17
+
18
+ def +(other)
19
+ raise ArgumentError, "Expected Arel node" unless Arel.arel_node?(other)
20
+
21
+ Fragments.new([self, other])
22
+ end
17
23
  end
18
24
  end
19
25
  end
@@ -26,6 +26,10 @@ module Arel # :nodoc: all
26
26
  def able_to_type_cast?
27
27
  relation.respond_to?(:able_to_type_cast?) && relation.able_to_type_cast?
28
28
  end
29
+
30
+ def to_cte
31
+ Arel::Nodes::Cte.new(name, relation)
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -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
- @relation = nil
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 &&
data/lib/arel/nodes.rb CHANGED
@@ -8,6 +8,7 @@ require "arel/nodes/select_core"
8
8
  require "arel/nodes/insert_statement"
9
9
  require "arel/nodes/update_statement"
10
10
  require "arel/nodes/bind_param"
11
+ require "arel/nodes/fragments"
11
12
 
12
13
  # terminal
13
14
 
@@ -28,6 +29,7 @@ require "arel/nodes/with"
28
29
  # binary
29
30
  require "arel/nodes/binary"
30
31
  require "arel/nodes/equality"
32
+ require "arel/nodes/filter"
31
33
  require "arel/nodes/in"
32
34
  require "arel/nodes/join_source"
33
35
  require "arel/nodes/delete_statement"
@@ -37,6 +39,7 @@ require "arel/nodes/unary_operation"
37
39
  require "arel/nodes/over"
38
40
  require "arel/nodes/matches"
39
41
  require "arel/nodes/regexp"
42
+ require "arel/nodes/cte"
40
43
 
41
44
  # nary
42
45
  require "arel/nodes/and"
@@ -62,9 +65,11 @@ require "arel/nodes/inner_join"
62
65
  require "arel/nodes/outer_join"
63
66
  require "arel/nodes/right_outer_join"
64
67
  require "arel/nodes/string_join"
68
+ require "arel/nodes/leading_join"
65
69
 
66
70
  require "arel/nodes/comment"
67
71
 
68
72
  require "arel/nodes/sql_literal"
73
+ require "arel/nodes/bound_sql_literal"
69
74
 
70
75
  require "arel/nodes/casted"
@@ -39,7 +39,11 @@ module Arel # :nodoc: all
39
39
  self.in([])
40
40
  elsif open_ended?(other.begin)
41
41
  if open_ended?(other.end)
42
- not_in([])
42
+ if infinity?(other.begin) == 1 || infinity?(other.end) == -1
43
+ self.in([])
44
+ else
45
+ not_in([])
46
+ end
43
47
  elsif other.exclude_end?
44
48
  lt(other.end)
45
49
  else
@@ -49,10 +53,12 @@ module Arel # :nodoc: all
49
53
  gteq(other.begin)
50
54
  elsif other.exclude_end?
51
55
  gteq(other.begin).and(lt(other.end))
56
+ elsif other.begin == other.end
57
+ eq(other.begin)
52
58
  else
53
59
  left = quoted_node(other.begin)
54
60
  right = quoted_node(other.end)
55
- Nodes::Between.new(self, left.and(right))
61
+ Nodes::Between.new(self, Nodes::And.new([left, right]))
56
62
  end
57
63
  end
58
64
 
@@ -80,7 +86,11 @@ module Arel # :nodoc: all
80
86
  not_in([])
81
87
  elsif open_ended?(other.begin)
82
88
  if open_ended?(other.end)
83
- self.in([])
89
+ if infinity?(other.begin) == 1 || infinity?(other.end) == -1
90
+ not_in([])
91
+ else
92
+ self.in([])
93
+ end
84
94
  elsif other.exclude_end?
85
95
  gteq(other.end)
86
96
  else
@@ -7,10 +7,8 @@ module Arel # :nodoc: all
7
7
  STRING_OR_SYMBOL_CLASS = [Symbol, String]
8
8
 
9
9
  def initialize(table = nil)
10
- super()
11
- @ast = Nodes::SelectStatement.new
10
+ @ast = Nodes::SelectStatement.new(table)
12
11
  @ctx = @ast.cores.last
13
- from table
14
12
  end
15
13
 
16
14
  def initialize_copy(other)
@@ -98,7 +96,7 @@ module Arel # :nodoc: all
98
96
  end
99
97
 
100
98
  def froms
101
- @ast.cores.map { |x| x.from }.compact
99
+ @ast.cores.filter_map { |x| x.from }
102
100
  end
103
101
 
104
102
  def join(relation, klass = Nodes::InnerJoin)
@@ -183,6 +181,14 @@ module Arel # :nodoc: all
183
181
  @ast.orders
184
182
  end
185
183
 
184
+ def where(expr)
185
+ if Arel::TreeManager === expr
186
+ expr = expr.ast
187
+ end
188
+ @ctx.wheres << expr
189
+ self
190
+ end
191
+
186
192
  def where_sql(engine = Table.engine)
187
193
  return if @ctx.wheres.empty?
188
194
 
data/lib/arel/table.rb CHANGED
@@ -2,20 +2,23 @@
2
2
 
3
3
  module Arel # :nodoc: all
4
4
  class Table
5
- include Arel::Crud
6
5
  include Arel::FactoryMethods
7
6
  include Arel::AliasPredication
8
7
 
9
8
  @engine = nil
10
9
  class << self; attr_accessor :engine; end
11
10
 
12
- attr_accessor :name, :table_alias
13
-
14
- # TableAlias and Table both have a #table_name which is the name of the underlying table
15
- alias :table_name :name
11
+ attr_accessor :name
12
+ attr_reader :table_alias
16
13
 
17
14
  def initialize(name, as: nil, klass: nil, type_caster: klass&.type_caster)
18
- @name = name.to_s
15
+ @name =
16
+ case name
17
+ when Symbol then name.to_s
18
+ else
19
+ name
20
+ end
21
+
19
22
  @klass = klass
20
23
  @type_caster = type_caster
21
24
 
@@ -21,7 +21,11 @@ module Arel # :nodoc: all
21
21
  end
22
22
 
23
23
  def key=(key)
24
- @ast.key = Nodes.build_quoted(key)
24
+ @ast.key = if key.is_a?(Array)
25
+ key.map { |k| Nodes.build_quoted(k) }
26
+ else
27
+ Nodes.build_quoted(key)
28
+ end
25
29
  end
26
30
 
27
31
  def key
@@ -40,10 +44,6 @@ module Arel # :nodoc: all
40
44
 
41
45
  attr_reader :ast
42
46
 
43
- def initialize
44
- @ctx = nil
45
- end
46
-
47
47
  def to_dot
48
48
  collector = Arel::Collectors::PlainString.new
49
49
  collector = Visitors::Dot.new.accept @ast, collector
@@ -60,13 +60,5 @@ module Arel # :nodoc: all
60
60
  super
61
61
  @ast = @ast.clone
62
62
  end
63
-
64
- def where(expr)
65
- if Arel::TreeManager === expr
66
- expr = expr.ast
67
- end
68
- @ctx.wheres << expr
69
- self
70
- end
71
63
  end
72
64
  end
@@ -4,10 +4,8 @@ module Arel # :nodoc: all
4
4
  class UpdateManager < Arel::TreeManager
5
5
  include TreeManager::StatementMethods
6
6
 
7
- def initialize
8
- super
9
- @ast = Nodes::UpdateStatement.new
10
- @ctx = @ast
7
+ def initialize(table = nil)
8
+ @ast = Nodes::UpdateStatement.new(table)
11
9
  end
12
10
 
13
11
  ###
@@ -30,5 +28,21 @@ module Arel # :nodoc: all
30
28
  end
31
29
  self
32
30
  end
31
+
32
+ def group(columns)
33
+ columns.each do |column|
34
+ column = Nodes::SqlLiteral.new(column) if String === column
35
+ column = Nodes::SqlLiteral.new(column.to_s) if Symbol === column
36
+
37
+ @ast.groups.push Nodes::Group.new column
38
+ end
39
+
40
+ self
41
+ end
42
+
43
+ def having(expr)
44
+ @ast.havings << expr
45
+ self
46
+ end
33
47
  end
34
48
  end