activerecord 5.0.7.2 → 5.1.0.beta1

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 (216) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +389 -2252
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/examples/performance.rb +28 -28
  6. data/examples/simple.rb +3 -3
  7. data/lib/active_record.rb +20 -20
  8. data/lib/active_record/aggregations.rb +244 -244
  9. data/lib/active_record/association_relation.rb +5 -5
  10. data/lib/active_record/associations.rb +1579 -1569
  11. data/lib/active_record/associations/alias_tracker.rb +1 -1
  12. data/lib/active_record/associations/association.rb +23 -15
  13. data/lib/active_record/associations/association_scope.rb +83 -81
  14. data/lib/active_record/associations/belongs_to_association.rb +0 -1
  15. data/lib/active_record/associations/builder/belongs_to.rb +16 -14
  16. data/lib/active_record/associations/builder/collection_association.rb +1 -2
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +27 -27
  18. data/lib/active_record/associations/collection_association.rb +74 -241
  19. data/lib/active_record/associations/collection_proxy.rb +144 -70
  20. data/lib/active_record/associations/has_many_association.rb +15 -19
  21. data/lib/active_record/associations/has_many_through_association.rb +12 -5
  22. data/lib/active_record/associations/has_one_association.rb +22 -28
  23. data/lib/active_record/associations/has_one_through_association.rb +5 -1
  24. data/lib/active_record/associations/join_dependency.rb +117 -115
  25. data/lib/active_record/associations/join_dependency/join_association.rb +16 -13
  26. data/lib/active_record/associations/join_dependency/join_base.rb +1 -1
  27. data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
  28. data/lib/active_record/associations/preloader.rb +94 -94
  29. data/lib/active_record/associations/preloader/association.rb +87 -64
  30. data/lib/active_record/associations/preloader/belongs_to.rb +0 -2
  31. data/lib/active_record/associations/preloader/collection_association.rb +6 -6
  32. data/lib/active_record/associations/preloader/has_many.rb +0 -2
  33. data/lib/active_record/associations/preloader/singular_association.rb +6 -8
  34. data/lib/active_record/associations/preloader/through_association.rb +34 -41
  35. data/lib/active_record/associations/singular_association.rb +8 -25
  36. data/lib/active_record/associations/through_association.rb +3 -6
  37. data/lib/active_record/attribute.rb +98 -71
  38. data/lib/active_record/attribute/user_provided_default.rb +4 -2
  39. data/lib/active_record/attribute_assignment.rb +61 -61
  40. data/lib/active_record/attribute_decorators.rb +35 -13
  41. data/lib/active_record/attribute_methods.rb +56 -65
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -7
  43. data/lib/active_record/attribute_methods/dirty.rb +216 -34
  44. data/lib/active_record/attribute_methods/primary_key.rb +78 -73
  45. data/lib/active_record/attribute_methods/read.rb +39 -35
  46. data/lib/active_record/attribute_methods/serialization.rb +7 -7
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +35 -58
  48. data/lib/active_record/attribute_methods/write.rb +36 -30
  49. data/lib/active_record/attribute_mutation_tracker.rb +53 -10
  50. data/lib/active_record/attribute_set.rb +9 -6
  51. data/lib/active_record/attribute_set/builder.rb +41 -49
  52. data/lib/active_record/attribute_set/yaml_encoder.rb +41 -0
  53. data/lib/active_record/attributes.rb +21 -21
  54. data/lib/active_record/autosave_association.rb +13 -13
  55. data/lib/active_record/base.rb +24 -22
  56. data/lib/active_record/callbacks.rb +52 -14
  57. data/lib/active_record/coders/yaml_column.rb +9 -11
  58. data/lib/active_record/collection_cache_key.rb +6 -17
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +320 -278
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +1 -3
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +22 -34
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -27
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +44 -57
  64. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +9 -19
  65. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +78 -79
  66. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +53 -41
  67. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +99 -93
  68. data/lib/active_record/connection_adapters/abstract/transaction.rb +1 -5
  69. data/lib/active_record/connection_adapters/abstract_adapter.rb +156 -128
  70. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +424 -382
  71. data/lib/active_record/connection_adapters/column.rb +27 -5
  72. data/lib/active_record/connection_adapters/connection_specification.rb +128 -118
  73. data/lib/active_record/connection_adapters/mysql/column.rb +6 -31
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -43
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +22 -22
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +6 -12
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +49 -45
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +16 -19
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +49 -31
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +5 -6
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +24 -26
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +1 -28
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -35
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +3 -3
  85. data/lib/active_record/connection_adapters/postgresql/oid.rb +22 -21
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +9 -9
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +5 -3
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -1
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +1 -1
  90. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -2
  91. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -3
  92. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +16 -16
  93. data/lib/active_record/connection_adapters/postgresql/oid/{rails_5_1_point.rb → legacy_point.rb} +9 -16
  94. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  95. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +13 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +28 -8
  97. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +28 -30
  98. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +51 -51
  100. data/lib/active_record/connection_adapters/postgresql/quoting.rb +38 -36
  101. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +15 -0
  102. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +37 -24
  103. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -23
  104. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +161 -170
  105. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +4 -4
  106. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -7
  107. data/lib/active_record/connection_adapters/postgresql_adapter.rb +179 -152
  108. data/lib/active_record/connection_adapters/schema_cache.rb +16 -7
  109. data/lib/active_record/connection_adapters/sql_type_metadata.rb +3 -3
  110. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +1 -1
  111. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +16 -20
  112. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +1 -8
  113. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +28 -0
  114. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +17 -0
  115. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +187 -130
  116. data/lib/active_record/connection_adapters/statement_pool.rb +7 -7
  117. data/lib/active_record/connection_handling.rb +14 -26
  118. data/lib/active_record/core.rb +110 -93
  119. data/lib/active_record/counter_cache.rb +62 -13
  120. data/lib/active_record/define_callbacks.rb +20 -0
  121. data/lib/active_record/dynamic_matchers.rb +80 -79
  122. data/lib/active_record/enum.rb +8 -6
  123. data/lib/active_record/errors.rb +58 -15
  124. data/lib/active_record/explain.rb +1 -2
  125. data/lib/active_record/explain_registry.rb +1 -1
  126. data/lib/active_record/explain_subscriber.rb +7 -4
  127. data/lib/active_record/fixture_set/file.rb +11 -8
  128. data/lib/active_record/fixtures.rb +66 -53
  129. data/lib/active_record/gem_version.rb +3 -3
  130. data/lib/active_record/inheritance.rb +93 -79
  131. data/lib/active_record/integration.rb +7 -7
  132. data/lib/active_record/internal_metadata.rb +3 -16
  133. data/lib/active_record/legacy_yaml_adapter.rb +1 -1
  134. data/lib/active_record/locking/optimistic.rb +64 -56
  135. data/lib/active_record/locking/pessimistic.rb +10 -1
  136. data/lib/active_record/log_subscriber.rb +29 -29
  137. data/lib/active_record/migration.rb +155 -172
  138. data/lib/active_record/migration/command_recorder.rb +94 -94
  139. data/lib/active_record/migration/compatibility.rb +76 -37
  140. data/lib/active_record/migration/join_table.rb +6 -6
  141. data/lib/active_record/model_schema.rb +85 -119
  142. data/lib/active_record/nested_attributes.rb +200 -199
  143. data/lib/active_record/null_relation.rb +10 -33
  144. data/lib/active_record/persistence.rb +45 -38
  145. data/lib/active_record/query_cache.rb +4 -8
  146. data/lib/active_record/querying.rb +2 -3
  147. data/lib/active_record/railtie.rb +16 -17
  148. data/lib/active_record/railties/controller_runtime.rb +6 -2
  149. data/lib/active_record/railties/databases.rake +125 -140
  150. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  151. data/lib/active_record/readonly_attributes.rb +2 -2
  152. data/lib/active_record/reflection.rb +79 -96
  153. data/lib/active_record/relation.rb +72 -115
  154. data/lib/active_record/relation/batches.rb +87 -58
  155. data/lib/active_record/relation/batches/batch_enumerator.rb +1 -1
  156. data/lib/active_record/relation/calculations.rb +154 -160
  157. data/lib/active_record/relation/delegation.rb +30 -29
  158. data/lib/active_record/relation/finder_methods.rb +195 -226
  159. data/lib/active_record/relation/merger.rb +58 -62
  160. data/lib/active_record/relation/predicate_builder.rb +92 -89
  161. data/lib/active_record/relation/predicate_builder/array_handler.rb +7 -5
  162. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +23 -23
  163. data/lib/active_record/relation/predicate_builder/base_handler.rb +3 -1
  164. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +0 -8
  165. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +12 -10
  166. data/lib/active_record/relation/predicate_builder/range_handler.rb +0 -8
  167. data/lib/active_record/relation/query_attribute.rb +1 -1
  168. data/lib/active_record/relation/query_methods.rb +247 -295
  169. data/lib/active_record/relation/record_fetch_warning.rb +3 -3
  170. data/lib/active_record/relation/spawn_methods.rb +4 -5
  171. data/lib/active_record/relation/where_clause.rb +79 -65
  172. data/lib/active_record/relation/where_clause_factory.rb +47 -8
  173. data/lib/active_record/result.rb +29 -31
  174. data/lib/active_record/runtime_registry.rb +3 -3
  175. data/lib/active_record/sanitization.rb +182 -197
  176. data/lib/active_record/schema.rb +3 -3
  177. data/lib/active_record/schema_dumper.rb +14 -37
  178. data/lib/active_record/schema_migration.rb +3 -3
  179. data/lib/active_record/scoping.rb +9 -10
  180. data/lib/active_record/scoping/default.rb +87 -91
  181. data/lib/active_record/scoping/named.rb +16 -28
  182. data/lib/active_record/secure_token.rb +2 -2
  183. data/lib/active_record/statement_cache.rb +13 -15
  184. data/lib/active_record/store.rb +31 -32
  185. data/lib/active_record/suppressor.rb +2 -1
  186. data/lib/active_record/table_metadata.rb +9 -5
  187. data/lib/active_record/tasks/database_tasks.rb +72 -65
  188. data/lib/active_record/tasks/mysql_database_tasks.rb +75 -72
  189. data/lib/active_record/tasks/postgresql_database_tasks.rb +53 -48
  190. data/lib/active_record/tasks/sqlite_database_tasks.rb +18 -16
  191. data/lib/active_record/timestamp.rb +39 -25
  192. data/lib/active_record/touch_later.rb +1 -2
  193. data/lib/active_record/transactions.rb +98 -110
  194. data/lib/active_record/type.rb +17 -13
  195. data/lib/active_record/type/adapter_specific_registry.rb +46 -42
  196. data/lib/active_record/type/decimal_without_scale.rb +9 -0
  197. data/lib/active_record/type/hash_lookup_type_map.rb +3 -3
  198. data/lib/active_record/type/serialized.rb +8 -8
  199. data/lib/active_record/type/text.rb +9 -0
  200. data/lib/active_record/type/time.rb +0 -1
  201. data/lib/active_record/type/type_map.rb +11 -15
  202. data/lib/active_record/type/unsigned_integer.rb +15 -0
  203. data/lib/active_record/type_caster.rb +2 -2
  204. data/lib/active_record/type_caster/connection.rb +8 -6
  205. data/lib/active_record/type_caster/map.rb +3 -1
  206. data/lib/active_record/validations.rb +4 -4
  207. data/lib/active_record/validations/associated.rb +1 -1
  208. data/lib/active_record/validations/presence.rb +2 -2
  209. data/lib/active_record/validations/uniqueness.rb +8 -39
  210. data/lib/active_record/version.rb +1 -1
  211. data/lib/rails/generators/active_record.rb +4 -4
  212. data/lib/rails/generators/active_record/migration.rb +2 -2
  213. data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -34
  214. data/lib/rails/generators/active_record/model/model_generator.rb +9 -9
  215. metadata +22 -13
  216. data/lib/active_record/relation/predicate_builder/class_handler.rb +0 -27
@@ -41,7 +41,6 @@ module ActiveRecord
41
41
  end
42
42
 
43
43
  class Transaction #:nodoc:
44
-
45
44
  attr_reader :connection, :state, :records, :savepoint_name
46
45
  attr_writer :joinable
47
46
 
@@ -101,7 +100,6 @@ module ActiveRecord
101
100
  end
102
101
 
103
102
  class SavepointTransaction < Transaction
104
-
105
103
  def initialize(connection, savepoint_name, options, *args)
106
104
  super(connection, options, *args)
107
105
  if options[:isolation]
@@ -124,7 +122,6 @@ module ActiveRecord
124
122
  end
125
123
 
126
124
  class RealTransaction < Transaction
127
-
128
125
  def initialize(connection, options, *args)
129
126
  super
130
127
  if options[:isolation]
@@ -195,7 +192,7 @@ module ActiveRecord
195
192
  raise
196
193
  ensure
197
194
  unless error
198
- if Thread.current.status == 'aborting'
195
+ if Thread.current.status == "aborting"
199
196
  rollback_transaction if transaction
200
197
  else
201
198
  begin
@@ -226,7 +223,6 @@ module ActiveRecord
226
223
  return unless error.is_a?(ActiveRecord::PreparedStatementCacheExpired)
227
224
  @connection.clear_cache!
228
225
  end
229
-
230
226
  end
231
227
  end
232
228
  end
@@ -1,11 +1,11 @@
1
- require 'active_record/type'
2
- require 'active_record/connection_adapters/determine_if_preparable_visitor'
3
- require 'active_record/connection_adapters/schema_cache'
4
- require 'active_record/connection_adapters/sql_type_metadata'
5
- require 'active_record/connection_adapters/abstract/schema_dumper'
6
- require 'active_record/connection_adapters/abstract/schema_creation'
7
- require 'arel/collectors/bind'
8
- require 'arel/collectors/sql_string'
1
+ require "active_record/type"
2
+ require "active_record/connection_adapters/determine_if_preparable_visitor"
3
+ require "active_record/connection_adapters/schema_cache"
4
+ require "active_record/connection_adapters/sql_type_metadata"
5
+ require "active_record/connection_adapters/abstract/schema_dumper"
6
+ require "active_record/connection_adapters/abstract/schema_creation"
7
+ require "arel/collectors/bind"
8
+ require "arel/collectors/sql_string"
9
9
 
10
10
  module ActiveRecord
11
11
  module ConnectionAdapters # :nodoc:
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
  autoload :Column
15
15
  autoload :ConnectionSpecification
16
16
 
17
- autoload_at 'active_record/connection_adapters/abstract/schema_definitions' do
17
+ autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
18
18
  autoload :IndexDefinition
19
19
  autoload :ColumnDefinition
20
20
  autoload :ChangeColumnDefinition
@@ -25,11 +25,11 @@ module ActiveRecord
25
25
  autoload :ReferenceDefinition
26
26
  end
27
27
 
28
- autoload_at 'active_record/connection_adapters/abstract/connection_pool' do
28
+ autoload_at "active_record/connection_adapters/abstract/connection_pool" do
29
29
  autoload :ConnectionHandler
30
30
  end
31
31
 
32
- autoload_under 'abstract' do
32
+ autoload_under "abstract" do
33
33
  autoload :SchemaStatements
34
34
  autoload :DatabaseStatements
35
35
  autoload :DatabaseLimits
@@ -39,7 +39,7 @@ module ActiveRecord
39
39
  autoload :Savepoints
40
40
  end
41
41
 
42
- autoload_at 'active_record/connection_adapters/abstract/transaction' do
42
+ autoload_at "active_record/connection_adapters/abstract/transaction" do
43
43
  autoload :TransactionManager
44
44
  autoload :NullTransaction
45
45
  autoload :RealTransaction
@@ -106,7 +106,8 @@ module ActiveRecord
106
106
  @pool = nil
107
107
  @schema_cache = SchemaCache.new self
108
108
  @quoted_column_names, @quoted_table_names = {}, {}
109
- @visitor = arel_visitor
109
+ @visitor = arel_visitor
110
+ @lock = Monitor.new
110
111
 
111
112
  if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
112
113
  @prepared_statements = true
@@ -120,17 +121,17 @@ module ActiveRecord
120
121
  include Comparable
121
122
 
122
123
  def initialize(version_string)
123
- @version = version_string.split('.').map(&:to_i)
124
+ @version = version_string.split(".").map(&:to_i)
124
125
  end
125
126
 
126
127
  def <=>(version_string)
127
- @version <=> version_string.split('.').map(&:to_i)
128
+ @version <=> version_string.split(".").map(&:to_i)
128
129
  end
129
130
  end
130
131
 
131
132
  class BindCollector < Arel::Collectors::Bind
132
133
  def compile(bvs, conn)
133
- casted_binds = conn.prepare_binds_for_database(bvs)
134
+ casted_binds = bvs.map(&:value_for_database)
134
135
  super(casted_binds.map { |value| conn.quote(value) })
135
136
  end
136
137
  end
@@ -161,14 +162,22 @@ module ActiveRecord
161
162
  SchemaCreation.new self
162
163
  end
163
164
 
165
+ # Returns an array of +Column+ objects for the table specified by +table_name+.
166
+ def columns(table_name) # :nodoc:
167
+ table_name = table_name.to_s
168
+ column_definitions(table_name).map do |field|
169
+ new_column_from_field(table_name, field)
170
+ end
171
+ end
172
+
164
173
  # this method must only be called while holding connection pool's mutex
165
174
  def lease
166
175
  if in_use?
167
- msg = 'Cannot lease connection, '
176
+ msg = "Cannot lease connection, "
168
177
  if @owner == Thread.current
169
- msg << 'it is already leased by the current thread.'
178
+ msg << "it is already leased by the current thread."
170
179
  else
171
- msg << "it is already in use by a different thread: #{@owner}. " <<
180
+ msg << "it is already in use by a different thread: #{@owner}. " \
172
181
  "Current thread: #{Thread.current}."
173
182
  end
174
183
  raise ActiveRecordError, msg
@@ -186,14 +195,14 @@ module ActiveRecord
186
195
  def expire
187
196
  if in_use?
188
197
  if @owner != Thread.current
189
- raise ActiveRecordError, "Cannot expire connection, " <<
190
- "it is owned by a different thread: #{@owner}. " <<
198
+ raise ActiveRecordError, "Cannot expire connection, " \
199
+ "it is owned by a different thread: #{@owner}. " \
191
200
  "Current thread: #{Thread.current}."
192
201
  end
193
202
 
194
203
  @owner = nil
195
204
  else
196
- raise ActiveRecordError, 'Cannot expire connection, it is not currently leased.'
205
+ raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
197
206
  end
198
207
  end
199
208
 
@@ -206,7 +215,7 @@ module ActiveRecord
206
215
  @owner = Thread.current
207
216
  end
208
217
  else
209
- raise ActiveRecordError, 'Cannot steal connection, it is not currently leased.'
218
+ raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
210
219
  end
211
220
  end
212
221
 
@@ -228,11 +237,10 @@ module ActiveRecord
228
237
  false
229
238
  end
230
239
 
231
- # Can this adapter determine the primary key for tables not attached
232
- # to an Active Record class, such as join tables?
233
- def supports_primary_key?
234
- false
240
+ def supports_primary_key? # :nodoc:
241
+ true
235
242
  end
243
+ deprecate :supports_primary_key?
236
244
 
237
245
  # Does this adapter support DDL rollbacks in transactions? That is, would
238
246
  # CREATE TABLE or ALTER TABLE get rolled back by a transaction?
@@ -302,6 +310,12 @@ module ActiveRecord
302
310
  false
303
311
  end
304
312
 
313
+ # Does this adapter support creating foreign key constraints
314
+ # in the same statement as creating the table?
315
+ def supports_foreign_keys_in_create?
316
+ supports_foreign_keys?
317
+ end
318
+
305
319
  # Does this adapter support views?
306
320
  def supports_views?
307
321
  false
@@ -332,6 +346,11 @@ module ActiveRecord
332
346
  true
333
347
  end
334
348
 
349
+ # Does this adapter support virtual columns?
350
+ def supports_virtual_columns?
351
+ false
352
+ end
353
+
335
354
  # This is meant to be implemented by the adapters that support extensions
336
355
  def disable_extension(name)
337
356
  end
@@ -425,7 +444,7 @@ module ActiveRecord
425
444
 
426
445
  # Provides access to the underlying database driver for this adapter. For
427
446
  # example, this method returns a Mysql2::Client object in case of Mysql2Adapter,
428
- # and a PG::Connection object in case of PostgreSQLAdapter.
447
+ # and a PGconn object in case of PostgreSQLAdapter.
429
448
  #
430
449
  # This is useful for when you need to call a proprietary method such as
431
450
  # PostgreSQL's lo_* methods.
@@ -433,19 +452,15 @@ module ActiveRecord
433
452
  @connection
434
453
  end
435
454
 
436
- def case_sensitive_comparison(table, attribute, column, value)
437
- if value.nil?
438
- table[attribute].eq(value)
439
- else
440
- table[attribute].eq(Arel::Nodes::BindParam.new)
441
- end
455
+ def case_sensitive_comparison(table, attribute, column, value) # :nodoc:
456
+ table[attribute].eq(value)
442
457
  end
443
458
 
444
- def case_insensitive_comparison(table, attribute, column, value)
459
+ def case_insensitive_comparison(table, attribute, column, value) # :nodoc:
445
460
  if can_perform_case_insensitive_comparison_for?(column)
446
- table[attribute].lower.eq(table.lower(Arel::Nodes::BindParam.new))
461
+ table[attribute].lower.eq(table.lower(value))
447
462
  else
448
- table[attribute].eq(Arel::Nodes::BindParam.new)
463
+ table[attribute].eq(value)
449
464
  end
450
465
  end
451
466
 
@@ -495,117 +510,130 @@ module ActiveRecord
495
510
  result
496
511
  end
497
512
 
498
- protected
499
-
500
- def initialize_type_map(m) # :nodoc:
501
- register_class_with_limit m, %r(boolean)i, Type::Boolean
502
- register_class_with_limit m, %r(char)i, Type::String
503
- register_class_with_limit m, %r(binary)i, Type::Binary
504
- register_class_with_limit m, %r(text)i, Type::Text
505
- register_class_with_precision m, %r(date)i, Type::Date
506
- register_class_with_precision m, %r(time)i, Type::Time
507
- register_class_with_precision m, %r(datetime)i, Type::DateTime
508
- register_class_with_limit m, %r(float)i, Type::Float
509
- register_class_with_limit m, %r(int)i, Type::Integer
510
-
511
- m.alias_type %r(blob)i, 'binary'
512
- m.alias_type %r(clob)i, 'text'
513
- m.alias_type %r(timestamp)i, 'datetime'
514
- m.alias_type %r(numeric)i, 'decimal'
515
- m.alias_type %r(number)i, 'decimal'
516
- m.alias_type %r(double)i, 'float'
517
-
518
- m.register_type(%r(decimal)i) do |sql_type|
519
- scale = extract_scale(sql_type)
520
- precision = extract_precision(sql_type)
521
-
522
- if scale == 0
523
- # FIXME: Remove this class as well
524
- Type::DecimalWithoutScale.new(precision: precision)
525
- else
526
- Type::Decimal.new(precision: precision, scale: scale)
513
+ def default_index_type?(index) # :nodoc:
514
+ index.using.nil?
515
+ end
516
+
517
+ private
518
+
519
+ def initialize_type_map(m)
520
+ register_class_with_limit m, %r(boolean)i, Type::Boolean
521
+ register_class_with_limit m, %r(char)i, Type::String
522
+ register_class_with_limit m, %r(binary)i, Type::Binary
523
+ register_class_with_limit m, %r(text)i, Type::Text
524
+ register_class_with_precision m, %r(date)i, Type::Date
525
+ register_class_with_precision m, %r(time)i, Type::Time
526
+ register_class_with_precision m, %r(datetime)i, Type::DateTime
527
+ register_class_with_limit m, %r(float)i, Type::Float
528
+ register_class_with_limit m, %r(int)i, Type::Integer
529
+
530
+ m.alias_type %r(blob)i, "binary"
531
+ m.alias_type %r(clob)i, "text"
532
+ m.alias_type %r(timestamp)i, "datetime"
533
+ m.alias_type %r(numeric)i, "decimal"
534
+ m.alias_type %r(number)i, "decimal"
535
+ m.alias_type %r(double)i, "float"
536
+
537
+ m.register_type(%r(decimal)i) do |sql_type|
538
+ scale = extract_scale(sql_type)
539
+ precision = extract_precision(sql_type)
540
+
541
+ if scale == 0
542
+ # FIXME: Remove this class as well
543
+ Type::DecimalWithoutScale.new(precision: precision)
544
+ else
545
+ Type::Decimal.new(precision: precision, scale: scale)
546
+ end
527
547
  end
528
548
  end
529
- end
530
549
 
531
- def reload_type_map # :nodoc:
532
- type_map.clear
533
- initialize_type_map(type_map)
534
- end
550
+ def reload_type_map
551
+ type_map.clear
552
+ initialize_type_map(type_map)
553
+ end
535
554
 
536
- def register_class_with_limit(mapping, key, klass) # :nodoc:
537
- mapping.register_type(key) do |*args|
538
- limit = extract_limit(args.last)
539
- klass.new(limit: limit)
555
+ def register_class_with_limit(mapping, key, klass)
556
+ mapping.register_type(key) do |*args|
557
+ limit = extract_limit(args.last)
558
+ klass.new(limit: limit)
559
+ end
540
560
  end
541
- end
542
561
 
543
- def register_class_with_precision(mapping, key, klass) # :nodoc:
544
- mapping.register_type(key) do |*args|
545
- precision = extract_precision(args.last)
546
- klass.new(precision: precision)
562
+ def register_class_with_precision(mapping, key, klass)
563
+ mapping.register_type(key) do |*args|
564
+ precision = extract_precision(args.last)
565
+ klass.new(precision: precision)
566
+ end
547
567
  end
548
- end
549
568
 
550
- def extract_scale(sql_type) # :nodoc:
551
- case sql_type
569
+ def extract_scale(sql_type)
570
+ case sql_type
552
571
  when /\((\d+)\)/ then 0
553
572
  when /\((\d+)(,(\d+))\)/ then $3.to_i
573
+ end
554
574
  end
555
- end
556
575
 
557
- def extract_precision(sql_type) # :nodoc:
558
- $1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
559
- end
560
-
561
- def extract_limit(sql_type) # :nodoc:
562
- case sql_type
563
- when /^bigint/i
564
- 8
565
- when /\((.*)\)/
566
- $1.to_i
576
+ def extract_precision(sql_type)
577
+ $1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
567
578
  end
568
- end
569
579
 
570
- def translate_exception_class(e, sql)
571
- begin
572
- message = "#{e.class.name}: #{e.message}: #{sql}"
573
- rescue Encoding::CompatibilityError
574
- message = "#{e.class.name}: #{e.message.force_encoding sql.encoding}: #{sql}"
580
+ def extract_limit(sql_type)
581
+ case sql_type
582
+ when /^bigint/i
583
+ 8
584
+ when /\((.*)\)/
585
+ $1.to_i
586
+ end
575
587
  end
576
588
 
577
- exception = translate_exception(e, message)
578
- exception.set_backtrace e.backtrace
579
- exception
580
- end
589
+ def translate_exception_class(e, sql)
590
+ begin
591
+ message = "#{e.class.name}: #{e.message}: #{sql}"
592
+ rescue Encoding::CompatibilityError
593
+ message = "#{e.class.name}: #{e.message.force_encoding sql.encoding}: #{sql}"
594
+ end
581
595
 
582
- def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil)
583
- @instrumenter.instrument(
584
- "sql.active_record",
585
- sql: sql,
586
- name: name,
587
- binds: binds,
588
- type_casted_binds: type_casted_binds,
589
- statement_name: statement_name,
590
- connection_id: object_id) { yield }
591
- rescue => e
592
- raise translate_exception_class(e, sql)
593
- end
596
+ exception = translate_exception(e, message)
597
+ exception.set_backtrace e.backtrace
598
+ exception
599
+ end
594
600
 
595
- def translate_exception(exception, message)
596
- # override in derived class
597
- ActiveRecord::StatementInvalid.new(message)
598
- end
601
+ def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) # :doc:
602
+ @instrumenter.instrument(
603
+ "sql.active_record",
604
+ sql: sql,
605
+ name: name,
606
+ binds: binds,
607
+ type_casted_binds: type_casted_binds,
608
+ statement_name: statement_name,
609
+ connection_id: object_id) do
610
+ @lock.synchronize do
611
+ yield
612
+ end
613
+ end
614
+ rescue => e
615
+ raise translate_exception_class(e, sql)
616
+ end
599
617
 
600
- def without_prepared_statement?(binds)
601
- !prepared_statements || binds.empty?
602
- end
618
+ def translate_exception(exception, message)
619
+ # override in derived class
620
+ case exception
621
+ when RuntimeError
622
+ exception
623
+ else
624
+ ActiveRecord::StatementInvalid.new(message)
625
+ end
626
+ end
603
627
 
604
- def column_for(table_name, column_name) # :nodoc:
605
- column_name = column_name.to_s
606
- columns(table_name).detect { |c| c.name == column_name } ||
607
- raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
608
- end
628
+ def without_prepared_statement?(binds)
629
+ !prepared_statements || binds.empty?
630
+ end
631
+
632
+ def column_for(table_name, column_name)
633
+ column_name = column_name.to_s
634
+ columns(table_name).detect { |c| c.name == column_name } ||
635
+ raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
636
+ end
609
637
  end
610
638
  end
611
639
  end