activerecord 6.1.7.6 → 7.0.0

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.

Files changed (238) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1055 -1180
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/lib/active_record/aggregations.rb +1 -1
  6. data/lib/active_record/association_relation.rb +0 -10
  7. data/lib/active_record/associations/association.rb +33 -17
  8. data/lib/active_record/associations/association_scope.rb +1 -3
  9. data/lib/active_record/associations/belongs_to_association.rb +15 -4
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
  11. data/lib/active_record/associations/builder/association.rb +8 -2
  12. data/lib/active_record/associations/builder/belongs_to.rb +19 -6
  13. data/lib/active_record/associations/builder/collection_association.rb +10 -3
  14. data/lib/active_record/associations/builder/has_many.rb +3 -2
  15. data/lib/active_record/associations/builder/has_one.rb +2 -1
  16. data/lib/active_record/associations/builder/singular_association.rb +2 -2
  17. data/lib/active_record/associations/collection_association.rb +18 -19
  18. data/lib/active_record/associations/collection_proxy.rb +8 -3
  19. data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
  20. data/lib/active_record/associations/has_many_association.rb +1 -1
  21. data/lib/active_record/associations/has_many_through_association.rb +2 -1
  22. data/lib/active_record/associations/has_one_association.rb +10 -7
  23. data/lib/active_record/associations/has_one_through_association.rb +1 -1
  24. data/lib/active_record/associations/join_dependency.rb +6 -2
  25. data/lib/active_record/associations/preloader/association.rb +186 -52
  26. data/lib/active_record/associations/preloader/batch.rb +48 -0
  27. data/lib/active_record/associations/preloader/branch.rb +147 -0
  28. data/lib/active_record/associations/preloader/through_association.rb +49 -13
  29. data/lib/active_record/associations/preloader.rb +39 -113
  30. data/lib/active_record/associations/singular_association.rb +8 -2
  31. data/lib/active_record/associations/through_association.rb +3 -3
  32. data/lib/active_record/associations.rb +90 -82
  33. data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
  34. data/lib/active_record/attribute_assignment.rb +1 -1
  35. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
  36. data/lib/active_record/attribute_methods/dirty.rb +49 -16
  37. data/lib/active_record/attribute_methods/primary_key.rb +2 -2
  38. data/lib/active_record/attribute_methods/query.rb +2 -2
  39. data/lib/active_record/attribute_methods/read.rb +7 -5
  40. data/lib/active_record/attribute_methods/serialization.rb +66 -12
  41. data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
  42. data/lib/active_record/attribute_methods/write.rb +7 -10
  43. data/lib/active_record/attribute_methods.rb +13 -14
  44. data/lib/active_record/attributes.rb +24 -35
  45. data/lib/active_record/autosave_association.rb +6 -21
  46. data/lib/active_record/base.rb +19 -1
  47. data/lib/active_record/callbacks.rb +2 -2
  48. data/lib/active_record/coders/yaml_column.rb +2 -14
  49. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +292 -0
  50. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +209 -0
  51. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +76 -0
  52. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +47 -561
  53. data/lib/active_record/connection_adapters/abstract/database_limits.rb +0 -17
  54. data/lib/active_record/connection_adapters/abstract/database_statements.rb +46 -22
  55. data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -12
  56. data/lib/active_record/connection_adapters/abstract/quoting.rb +43 -82
  57. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +4 -17
  58. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +34 -13
  59. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +69 -18
  60. data/lib/active_record/connection_adapters/abstract/transaction.rb +15 -22
  61. data/lib/active_record/connection_adapters/abstract_adapter.rb +149 -74
  62. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +97 -81
  63. data/lib/active_record/connection_adapters/column.rb +4 -0
  64. data/lib/active_record/connection_adapters/mysql/database_statements.rb +35 -23
  65. data/lib/active_record/connection_adapters/mysql/quoting.rb +35 -21
  66. data/lib/active_record/connection_adapters/mysql2_adapter.rb +12 -6
  67. data/lib/active_record/connection_adapters/pool_config.rb +7 -7
  68. data/lib/active_record/connection_adapters/postgresql/column.rb +17 -1
  69. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +19 -12
  70. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
  73. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +28 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +50 -76
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +32 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +21 -1
  81. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +22 -1
  82. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +25 -0
  83. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +27 -16
  84. data/lib/active_record/connection_adapters/postgresql_adapter.rb +207 -107
  85. data/lib/active_record/connection_adapters/schema_cache.rb +39 -38
  86. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +25 -19
  87. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +15 -16
  88. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +4 -2
  89. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +61 -30
  90. data/lib/active_record/connection_adapters.rb +6 -5
  91. data/lib/active_record/connection_handling.rb +47 -53
  92. data/lib/active_record/core.rb +121 -146
  93. data/lib/active_record/database_configurations/connection_url_resolver.rb +1 -1
  94. data/lib/active_record/database_configurations/database_config.rb +12 -9
  95. data/lib/active_record/database_configurations/hash_config.rb +63 -5
  96. data/lib/active_record/database_configurations/url_config.rb +2 -2
  97. data/lib/active_record/database_configurations.rb +15 -32
  98. data/lib/active_record/delegated_type.rb +52 -11
  99. data/lib/active_record/destroy_association_async_job.rb +1 -1
  100. data/lib/active_record/disable_joins_association_relation.rb +39 -0
  101. data/lib/active_record/dynamic_matchers.rb +1 -1
  102. data/lib/active_record/encryption/cipher/aes256_gcm.rb +98 -0
  103. data/lib/active_record/encryption/cipher.rb +53 -0
  104. data/lib/active_record/encryption/config.rb +44 -0
  105. data/lib/active_record/encryption/configurable.rb +61 -0
  106. data/lib/active_record/encryption/context.rb +35 -0
  107. data/lib/active_record/encryption/contexts.rb +72 -0
  108. data/lib/active_record/encryption/derived_secret_key_provider.rb +12 -0
  109. data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
  110. data/lib/active_record/encryption/encryptable_record.rb +208 -0
  111. data/lib/active_record/encryption/encrypted_attribute_type.rb +140 -0
  112. data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
  113. data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
  114. data/lib/active_record/encryption/encryptor.rb +155 -0
  115. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
  116. data/lib/active_record/encryption/errors.rb +15 -0
  117. data/lib/active_record/encryption/extended_deterministic_queries.rb +160 -0
  118. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
  119. data/lib/active_record/encryption/key.rb +28 -0
  120. data/lib/active_record/encryption/key_generator.rb +42 -0
  121. data/lib/active_record/encryption/key_provider.rb +46 -0
  122. data/lib/active_record/encryption/message.rb +33 -0
  123. data/lib/active_record/encryption/message_serializer.rb +90 -0
  124. data/lib/active_record/encryption/null_encryptor.rb +21 -0
  125. data/lib/active_record/encryption/properties.rb +76 -0
  126. data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
  127. data/lib/active_record/encryption/scheme.rb +99 -0
  128. data/lib/active_record/encryption.rb +55 -0
  129. data/lib/active_record/enum.rb +49 -42
  130. data/lib/active_record/errors.rb +67 -4
  131. data/lib/active_record/explain_registry.rb +11 -6
  132. data/lib/active_record/fixture_set/file.rb +15 -1
  133. data/lib/active_record/fixture_set/table_row.rb +41 -6
  134. data/lib/active_record/fixture_set/table_rows.rb +4 -4
  135. data/lib/active_record/fixtures.rb +17 -20
  136. data/lib/active_record/future_result.rb +139 -0
  137. data/lib/active_record/gem_version.rb +4 -4
  138. data/lib/active_record/inheritance.rb +55 -17
  139. data/lib/active_record/insert_all.rb +80 -14
  140. data/lib/active_record/integration.rb +4 -3
  141. data/lib/active_record/internal_metadata.rb +1 -5
  142. data/lib/active_record/legacy_yaml_adapter.rb +2 -39
  143. data/lib/active_record/locking/optimistic.rb +10 -9
  144. data/lib/active_record/locking/pessimistic.rb +9 -3
  145. data/lib/active_record/log_subscriber.rb +14 -3
  146. data/lib/active_record/middleware/database_selector/resolver.rb +6 -10
  147. data/lib/active_record/middleware/database_selector.rb +8 -3
  148. data/lib/active_record/middleware/shard_selector.rb +60 -0
  149. data/lib/active_record/migration/command_recorder.rb +4 -4
  150. data/lib/active_record/migration/compatibility.rb +89 -10
  151. data/lib/active_record/migration/join_table.rb +1 -1
  152. data/lib/active_record/migration.rb +110 -80
  153. data/lib/active_record/model_schema.rb +45 -58
  154. data/lib/active_record/nested_attributes.rb +13 -12
  155. data/lib/active_record/no_touching.rb +3 -3
  156. data/lib/active_record/null_relation.rb +2 -6
  157. data/lib/active_record/persistence.rb +219 -52
  158. data/lib/active_record/query_cache.rb +2 -2
  159. data/lib/active_record/query_logs.rb +138 -0
  160. data/lib/active_record/querying.rb +15 -5
  161. data/lib/active_record/railtie.rb +127 -17
  162. data/lib/active_record/railties/controller_runtime.rb +1 -1
  163. data/lib/active_record/railties/databases.rake +66 -129
  164. data/lib/active_record/readonly_attributes.rb +11 -0
  165. data/lib/active_record/reflection.rb +67 -50
  166. data/lib/active_record/relation/batches/batch_enumerator.rb +19 -5
  167. data/lib/active_record/relation/batches.rb +3 -3
  168. data/lib/active_record/relation/calculations.rb +40 -36
  169. data/lib/active_record/relation/delegation.rb +6 -6
  170. data/lib/active_record/relation/finder_methods.rb +31 -35
  171. data/lib/active_record/relation/merger.rb +20 -13
  172. data/lib/active_record/relation/predicate_builder.rb +1 -6
  173. data/lib/active_record/relation/query_attribute.rb +5 -11
  174. data/lib/active_record/relation/query_methods.rb +235 -63
  175. data/lib/active_record/relation/record_fetch_warning.rb +7 -9
  176. data/lib/active_record/relation/spawn_methods.rb +2 -2
  177. data/lib/active_record/relation/where_clause.rb +10 -19
  178. data/lib/active_record/relation.rb +169 -84
  179. data/lib/active_record/result.rb +17 -7
  180. data/lib/active_record/runtime_registry.rb +9 -13
  181. data/lib/active_record/sanitization.rb +11 -7
  182. data/lib/active_record/schema_dumper.rb +10 -3
  183. data/lib/active_record/schema_migration.rb +4 -4
  184. data/lib/active_record/scoping/default.rb +61 -12
  185. data/lib/active_record/scoping/named.rb +3 -11
  186. data/lib/active_record/scoping.rb +64 -34
  187. data/lib/active_record/serialization.rb +1 -1
  188. data/lib/active_record/signed_id.rb +1 -1
  189. data/lib/active_record/store.rb +1 -6
  190. data/lib/active_record/suppressor.rb +11 -15
  191. data/lib/active_record/tasks/database_tasks.rb +116 -58
  192. data/lib/active_record/tasks/mysql_database_tasks.rb +1 -1
  193. data/lib/active_record/tasks/postgresql_database_tasks.rb +19 -12
  194. data/lib/active_record/test_databases.rb +1 -1
  195. data/lib/active_record/test_fixtures.rb +9 -13
  196. data/lib/active_record/timestamp.rb +3 -4
  197. data/lib/active_record/transactions.rb +9 -14
  198. data/lib/active_record/translation.rb +2 -2
  199. data/lib/active_record/type/adapter_specific_registry.rb +32 -7
  200. data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
  201. data/lib/active_record/type/internal/timezone.rb +2 -2
  202. data/lib/active_record/type/serialized.rb +1 -1
  203. data/lib/active_record/type/type_map.rb +17 -20
  204. data/lib/active_record/type.rb +1 -2
  205. data/lib/active_record/validations/associated.rb +1 -1
  206. data/lib/active_record/validations/uniqueness.rb +1 -1
  207. data/lib/active_record.rb +204 -28
  208. data/lib/arel/attributes/attribute.rb +0 -8
  209. data/lib/arel/crud.rb +28 -22
  210. data/lib/arel/delete_manager.rb +18 -4
  211. data/lib/arel/filter_predications.rb +9 -0
  212. data/lib/arel/insert_manager.rb +2 -3
  213. data/lib/arel/nodes/casted.rb +1 -1
  214. data/lib/arel/nodes/delete_statement.rb +12 -13
  215. data/lib/arel/nodes/filter.rb +10 -0
  216. data/lib/arel/nodes/function.rb +1 -0
  217. data/lib/arel/nodes/insert_statement.rb +2 -2
  218. data/lib/arel/nodes/select_core.rb +2 -2
  219. data/lib/arel/nodes/select_statement.rb +2 -2
  220. data/lib/arel/nodes/update_statement.rb +8 -3
  221. data/lib/arel/nodes.rb +1 -0
  222. data/lib/arel/predications.rb +11 -3
  223. data/lib/arel/select_manager.rb +10 -4
  224. data/lib/arel/table.rb +0 -1
  225. data/lib/arel/tree_manager.rb +0 -12
  226. data/lib/arel/update_manager.rb +18 -4
  227. data/lib/arel/visitors/dot.rb +80 -90
  228. data/lib/arel/visitors/mysql.rb +8 -2
  229. data/lib/arel/visitors/postgresql.rb +0 -10
  230. data/lib/arel/visitors/to_sql.rb +58 -2
  231. data/lib/arel.rb +2 -1
  232. data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
  233. data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
  234. data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
  235. data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
  236. data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
  237. data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
  238. metadata +58 -14
@@ -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
4
+ module Nodes
5
+ class Filter < Binary
6
+ include Arel::WindowPredications
7
+ include Arel::AliasPredication
8
+ end
9
+ end
10
+ 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)
@@ -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
@@ -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
@@ -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
@@ -28,6 +28,7 @@ require "arel/nodes/with"
28
28
  # binary
29
29
  require "arel/nodes/binary"
30
30
  require "arel/nodes/equality"
31
+ require "arel/nodes/filter"
31
32
  require "arel/nodes/in"
32
33
  require "arel/nodes/join_source"
33
34
  require "arel/nodes/delete_statement"
@@ -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
@@ -52,7 +56,7 @@ module Arel # :nodoc: all
52
56
  else
53
57
  left = quoted_node(other.begin)
54
58
  right = quoted_node(other.end)
55
- Nodes::Between.new(self, left.and(right))
59
+ Nodes::Between.new(self, Nodes::And.new([left, right]))
56
60
  end
57
61
  end
58
62
 
@@ -80,7 +84,11 @@ module Arel # :nodoc: all
80
84
  not_in([])
81
85
  elsif open_ended?(other.begin)
82
86
  if open_ended?(other.end)
83
- self.in([])
87
+ if infinity?(other.begin) == 1 || infinity?(other.end) == -1
88
+ not_in([])
89
+ else
90
+ self.in([])
91
+ end
84
92
  elsif other.exclude_end?
85
93
  gteq(other.end)
86
94
  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,7 +2,6 @@
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
 
@@ -40,10 +40,6 @@ module Arel # :nodoc: all
40
40
 
41
41
  attr_reader :ast
42
42
 
43
- def initialize
44
- @ctx = nil
45
- end
46
-
47
43
  def to_dot
48
44
  collector = Arel::Collectors::PlainString.new
49
45
  collector = Visitors::Dot.new.accept @ast, collector
@@ -60,13 +56,5 @@ module Arel # :nodoc: all
60
56
  super
61
57
  @ast = @ast.clone
62
58
  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
59
  end
72
60
  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
@@ -31,6 +31,40 @@ module Arel # :nodoc: all
31
31
  end
32
32
 
33
33
  private
34
+ def visit_Arel_Nodes_Function(o)
35
+ visit_edge o, "expressions"
36
+ visit_edge o, "distinct"
37
+ visit_edge o, "alias"
38
+ end
39
+
40
+ def visit_Arel_Nodes_Unary(o)
41
+ visit_edge o, "expr"
42
+ end
43
+
44
+ def visit_Arel_Nodes_Binary(o)
45
+ visit_edge o, "left"
46
+ visit_edge o, "right"
47
+ end
48
+
49
+ def visit_Arel_Nodes_UnaryOperation(o)
50
+ visit_edge o, "operator"
51
+ visit_edge o, "expr"
52
+ end
53
+
54
+ def visit_Arel_Nodes_InfixOperation(o)
55
+ visit_edge o, "operator"
56
+ visit_edge o, "left"
57
+ visit_edge o, "right"
58
+ end
59
+
60
+ def visit__regexp(o)
61
+ visit_edge o, "left"
62
+ visit_edge o, "right"
63
+ visit_edge o, "case_sensitive"
64
+ end
65
+ alias :visit_Arel_Nodes_Regexp :visit__regexp
66
+ alias :visit_Arel_Nodes_NotRegexp :visit__regexp
67
+
34
68
  def visit_Arel_Nodes_Ordering(o)
35
69
  visit_edge o, "expr"
36
70
  end
@@ -53,71 +87,29 @@ module Arel # :nodoc: all
53
87
  visit_edge o, "left"
54
88
  end
55
89
 
56
- def visit_Arel_Nodes_InnerJoin(o)
57
- visit_edge o, "left"
58
- visit_edge o, "right"
59
- end
60
- alias :visit_Arel_Nodes_FullOuterJoin :visit_Arel_Nodes_InnerJoin
61
- alias :visit_Arel_Nodes_OuterJoin :visit_Arel_Nodes_InnerJoin
62
- alias :visit_Arel_Nodes_RightOuterJoin :visit_Arel_Nodes_InnerJoin
63
-
64
- def visit_Arel_Nodes_DeleteStatement(o)
65
- visit_edge o, "relation"
66
- visit_edge o, "wheres"
67
- end
68
-
69
- def unary(o)
70
- visit_edge o, "expr"
71
- end
72
- alias :visit_Arel_Nodes_Group :unary
73
- alias :visit_Arel_Nodes_Cube :unary
74
- alias :visit_Arel_Nodes_RollUp :unary
75
- alias :visit_Arel_Nodes_GroupingSet :unary
76
- alias :visit_Arel_Nodes_GroupingElement :unary
77
- alias :visit_Arel_Nodes_Grouping :unary
78
- alias :visit_Arel_Nodes_Having :unary
79
- alias :visit_Arel_Nodes_Limit :unary
80
- alias :visit_Arel_Nodes_Not :unary
81
- alias :visit_Arel_Nodes_Offset :unary
82
- alias :visit_Arel_Nodes_On :unary
83
- alias :visit_Arel_Nodes_UnqualifiedColumn :unary
84
- alias :visit_Arel_Nodes_OptimizerHints :unary
85
- alias :visit_Arel_Nodes_Preceding :unary
86
- alias :visit_Arel_Nodes_Following :unary
87
- alias :visit_Arel_Nodes_Rows :unary
88
- alias :visit_Arel_Nodes_Range :unary
89
-
90
- def window(o)
90
+ def visit_Arel_Nodes_Window(o)
91
91
  visit_edge o, "partitions"
92
92
  visit_edge o, "orders"
93
93
  visit_edge o, "framing"
94
94
  end
95
- alias :visit_Arel_Nodes_Window :window
96
95
 
97
- def named_window(o)
96
+ def visit_Arel_Nodes_NamedWindow(o)
98
97
  visit_edge o, "partitions"
99
98
  visit_edge o, "orders"
100
99
  visit_edge o, "framing"
101
100
  visit_edge o, "name"
102
101
  end
103
- alias :visit_Arel_Nodes_NamedWindow :named_window
104
102
 
105
- def function(o)
106
- visit_edge o, "expressions"
107
- visit_edge o, "distinct"
108
- visit_edge o, "alias"
103
+ def visit__no_edges(o)
104
+ # intentionally left blank
109
105
  end
110
- alias :visit_Arel_Nodes_Exists :function
111
- alias :visit_Arel_Nodes_Min :function
112
- alias :visit_Arel_Nodes_Max :function
113
- alias :visit_Arel_Nodes_Avg :function
114
- alias :visit_Arel_Nodes_Sum :function
106
+ alias :visit_Arel_Nodes_CurrentRow :visit__no_edges
107
+ alias :visit_Arel_Nodes_Distinct :visit__no_edges
115
108
 
116
- def extract(o)
109
+ def visit_Arel_Nodes_Extract(o)
117
110
  visit_edge o, "expressions"
118
111
  visit_edge o, "alias"
119
112
  end
120
- alias :visit_Arel_Nodes_Extract :extract
121
113
 
122
114
  def visit_Arel_Nodes_NamedFunction(o)
123
115
  visit_edge o, "name"
@@ -130,13 +122,19 @@ module Arel # :nodoc: all
130
122
  visit_edge o, "relation"
131
123
  visit_edge o, "columns"
132
124
  visit_edge o, "values"
125
+ visit_edge o, "select"
133
126
  end
134
127
 
135
128
  def visit_Arel_Nodes_SelectCore(o)
136
129
  visit_edge o, "source"
137
130
  visit_edge o, "projections"
138
131
  visit_edge o, "wheres"
139
- visit_edge o, "windows"
132
+ visit_edge o, "windows"
133
+ visit_edge o, "groups"
134
+ visit_edge o, "comment"
135
+ visit_edge o, "havings"
136
+ visit_edge o, "set_quantifier"
137
+ visit_edge o, "optimizer_hints"
140
138
  end
141
139
 
142
140
  def visit_Arel_Nodes_SelectStatement(o)
@@ -144,12 +142,27 @@ module Arel # :nodoc: all
144
142
  visit_edge o, "limit"
145
143
  visit_edge o, "orders"
146
144
  visit_edge o, "offset"
145
+ visit_edge o, "lock"
146
+ visit_edge o, "with"
147
147
  end
148
148
 
149
149
  def visit_Arel_Nodes_UpdateStatement(o)
150
150
  visit_edge o, "relation"
151
151
  visit_edge o, "wheres"
152
152
  visit_edge o, "values"
153
+ visit_edge o, "orders"
154
+ visit_edge o, "limit"
155
+ visit_edge o, "offset"
156
+ visit_edge o, "key"
157
+ end
158
+
159
+ def visit_Arel_Nodes_DeleteStatement(o)
160
+ visit_edge o, "relation"
161
+ visit_edge o, "wheres"
162
+ visit_edge o, "orders"
163
+ visit_edge o, "limit"
164
+ visit_edge o, "offset"
165
+ visit_edge o, "key"
153
166
  end
154
167
 
155
168
  def visit_Arel_Table(o)
@@ -167,47 +180,18 @@ module Arel # :nodoc: all
167
180
  visit_edge o, "attribute"
168
181
  end
169
182
 
170
- def visit_Arel_Attribute(o)
183
+ def visit_Arel_Attributes_Attribute(o)
171
184
  visit_edge o, "relation"
172
185
  visit_edge o, "name"
173
186
  end
174
- alias :visit_Arel_Attributes_Integer :visit_Arel_Attribute
175
- alias :visit_Arel_Attributes_Float :visit_Arel_Attribute
176
- alias :visit_Arel_Attributes_String :visit_Arel_Attribute
177
- alias :visit_Arel_Attributes_Time :visit_Arel_Attribute
178
- alias :visit_Arel_Attributes_Boolean :visit_Arel_Attribute
179
- alias :visit_Arel_Attributes_Attribute :visit_Arel_Attribute
180
187
 
181
- def nary(o)
182
- o.children.each_with_index do |x, i|
183
- edge(i) { visit x }
188
+ def visit__children(o)
189
+ o.children.each_with_index do |child, i|
190
+ edge(i) { visit child }
184
191
  end
185
192
  end
186
- alias :visit_Arel_Nodes_And :nary
187
-
188
- def binary(o)
189
- visit_edge o, "left"
190
- visit_edge o, "right"
191
- end
192
- alias :visit_Arel_Nodes_As :binary
193
- alias :visit_Arel_Nodes_Assignment :binary
194
- alias :visit_Arel_Nodes_Between :binary
195
- alias :visit_Arel_Nodes_Concat :binary
196
- alias :visit_Arel_Nodes_DoesNotMatch :binary
197
- alias :visit_Arel_Nodes_Equality :binary
198
- alias :visit_Arel_Nodes_GreaterThan :binary
199
- alias :visit_Arel_Nodes_GreaterThanOrEqual :binary
200
- alias :visit_Arel_Nodes_In :binary
201
- alias :visit_Arel_Nodes_JoinSource :binary
202
- alias :visit_Arel_Nodes_LessThan :binary
203
- alias :visit_Arel_Nodes_LessThanOrEqual :binary
204
- alias :visit_Arel_Nodes_IsNotDistinctFrom :binary
205
- alias :visit_Arel_Nodes_IsDistinctFrom :binary
206
- alias :visit_Arel_Nodes_Matches :binary
207
- alias :visit_Arel_Nodes_NotEqual :binary
208
- alias :visit_Arel_Nodes_NotIn :binary
209
- alias :visit_Arel_Nodes_Or :binary
210
- alias :visit_Arel_Nodes_Over :binary
193
+ alias :visit_Arel_Nodes_And :visit__children
194
+ alias :visit_Arel_Nodes_With :visit__children
211
195
 
212
196
  def visit_String(o)
213
197
  @node_stack.last.fields << o
@@ -225,22 +209,22 @@ module Arel # :nodoc: all
225
209
  alias :visit_Arel_Nodes_SqlLiteral :visit_String
226
210
 
227
211
  def visit_Arel_Nodes_BindParam(o)
228
- edge("value") { visit o.value }
212
+ visit_edge(o, "value")
229
213
  end
230
214
 
231
215
  def visit_ActiveModel_Attribute(o)
232
- edge("value_before_type_cast") { visit o.value_before_type_cast }
216
+ visit_edge(o, "value_before_type_cast")
233
217
  end
234
218
 
235
219
  def visit_Hash(o)
236
220
  o.each_with_index do |pair, i|
237
- edge("pair_#{i}") { visit pair }
221
+ edge("pair_#{i}") { visit pair }
238
222
  end
239
223
  end
240
224
 
241
225
  def visit_Array(o)
242
- o.each_with_index do |x, i|
243
- edge(i) { visit x }
226
+ o.each_with_index do |member, i|
227
+ edge(i) { visit member }
244
228
  end
245
229
  end
246
230
  alias :visit_Set :visit_Array
@@ -249,6 +233,12 @@ module Arel # :nodoc: all
249
233
  visit_edge(o, "values")
250
234
  end
251
235
 
236
+ def visit_Arel_Nodes_Case(o)
237
+ visit_edge(o, "case")
238
+ visit_edge(o, "conditions")
239
+ visit_edge(o, "default")
240
+ end
241
+
252
242
  def visit_edge(o, method)
253
243
  edge(method) { visit o.send(method) }
254
244
  end
@@ -58,11 +58,17 @@ module Arel # :nodoc: all
58
58
  infix_value o, collector, " NOT REGEXP "
59
59
  end
60
60
 
61
+ # no-op
62
+ def visit_Arel_Nodes_NullsFirst(o, collector)
63
+ visit o.expr, collector
64
+ end
65
+
61
66
  # In the simple case, MySQL allows us to place JOINs directly into the UPDATE
62
67
  # query. However, this does not allow for LIMIT, OFFSET and ORDER. To support
63
68
  # these, we must use a subquery.
64
69
  def prepare_update_statement(o)
65
- if o.offset || has_join_sources?(o) && has_limit_or_offset_or_orders?(o)
70
+ if o.offset || has_group_by_and_having?(o) ||
71
+ has_join_sources?(o) && has_limit_or_offset_or_orders?(o)
66
72
  super
67
73
  else
68
74
  o
@@ -70,7 +76,7 @@ module Arel # :nodoc: all
70
76
  end
71
77
  alias :prepare_delete_statement :prepare_update_statement
72
78
 
73
- # MySQL is too stupid to create a temporary table for use subquery, so we have
79
+ # MySQL doesn't automatically create a temporary table for use subquery, so we have
74
80
  # to give it some prompting in the form of a subsubquery.
75
81
  def build_subselect(key, o)
76
82
  subselect = super
@@ -78,16 +78,6 @@ module Arel # :nodoc: all
78
78
  visit o.right, collector
79
79
  end
80
80
 
81
- def visit_Arel_Nodes_NullsFirst(o, collector)
82
- visit o.expr, collector
83
- collector << " NULLS FIRST"
84
- end
85
-
86
- def visit_Arel_Nodes_NullsLast(o, collector)
87
- visit o.expr, collector
88
- collector << " NULLS LAST"
89
- end
90
-
91
81
  BIND_BLOCK = proc { |i| "$#{i}" }
92
82
  private_constant :BIND_BLOCK
93
83