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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +389 -2252
- data/MIT-LICENSE +1 -1
- data/README.rdoc +1 -1
- data/examples/performance.rb +28 -28
- data/examples/simple.rb +3 -3
- data/lib/active_record.rb +20 -20
- data/lib/active_record/aggregations.rb +244 -244
- data/lib/active_record/association_relation.rb +5 -5
- data/lib/active_record/associations.rb +1579 -1569
- data/lib/active_record/associations/alias_tracker.rb +1 -1
- data/lib/active_record/associations/association.rb +23 -15
- data/lib/active_record/associations/association_scope.rb +83 -81
- data/lib/active_record/associations/belongs_to_association.rb +0 -1
- data/lib/active_record/associations/builder/belongs_to.rb +16 -14
- data/lib/active_record/associations/builder/collection_association.rb +1 -2
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +27 -27
- data/lib/active_record/associations/collection_association.rb +74 -241
- data/lib/active_record/associations/collection_proxy.rb +144 -70
- data/lib/active_record/associations/has_many_association.rb +15 -19
- data/lib/active_record/associations/has_many_through_association.rb +12 -5
- data/lib/active_record/associations/has_one_association.rb +22 -28
- data/lib/active_record/associations/has_one_through_association.rb +5 -1
- data/lib/active_record/associations/join_dependency.rb +117 -115
- data/lib/active_record/associations/join_dependency/join_association.rb +16 -13
- data/lib/active_record/associations/join_dependency/join_base.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
- data/lib/active_record/associations/preloader.rb +94 -94
- data/lib/active_record/associations/preloader/association.rb +87 -64
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -2
- data/lib/active_record/associations/preloader/collection_association.rb +6 -6
- data/lib/active_record/associations/preloader/has_many.rb +0 -2
- data/lib/active_record/associations/preloader/singular_association.rb +6 -8
- data/lib/active_record/associations/preloader/through_association.rb +34 -41
- data/lib/active_record/associations/singular_association.rb +8 -25
- data/lib/active_record/associations/through_association.rb +3 -6
- data/lib/active_record/attribute.rb +98 -71
- data/lib/active_record/attribute/user_provided_default.rb +4 -2
- data/lib/active_record/attribute_assignment.rb +61 -61
- data/lib/active_record/attribute_decorators.rb +35 -13
- data/lib/active_record/attribute_methods.rb +56 -65
- data/lib/active_record/attribute_methods/before_type_cast.rb +7 -7
- data/lib/active_record/attribute_methods/dirty.rb +216 -34
- data/lib/active_record/attribute_methods/primary_key.rb +78 -73
- data/lib/active_record/attribute_methods/read.rb +39 -35
- data/lib/active_record/attribute_methods/serialization.rb +7 -7
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +35 -58
- data/lib/active_record/attribute_methods/write.rb +36 -30
- data/lib/active_record/attribute_mutation_tracker.rb +53 -10
- data/lib/active_record/attribute_set.rb +9 -6
- data/lib/active_record/attribute_set/builder.rb +41 -49
- data/lib/active_record/attribute_set/yaml_encoder.rb +41 -0
- data/lib/active_record/attributes.rb +21 -21
- data/lib/active_record/autosave_association.rb +13 -13
- data/lib/active_record/base.rb +24 -22
- data/lib/active_record/callbacks.rb +52 -14
- data/lib/active_record/coders/yaml_column.rb +9 -11
- data/lib/active_record/collection_cache_key.rb +6 -17
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +320 -278
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +1 -3
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +22 -34
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -27
- data/lib/active_record/connection_adapters/abstract/quoting.rb +44 -57
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +9 -19
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +78 -79
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +53 -41
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +99 -93
- data/lib/active_record/connection_adapters/abstract/transaction.rb +1 -5
- data/lib/active_record/connection_adapters/abstract_adapter.rb +156 -128
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +424 -382
- data/lib/active_record/connection_adapters/column.rb +27 -5
- data/lib/active_record/connection_adapters/connection_specification.rb +128 -118
- data/lib/active_record/connection_adapters/mysql/column.rb +6 -31
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -43
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +22 -22
- data/lib/active_record/connection_adapters/mysql/quoting.rb +6 -12
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +49 -45
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +16 -19
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +49 -31
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +5 -6
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +24 -26
- data/lib/active_record/connection_adapters/postgresql/column.rb +1 -28
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -35
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid.rb +22 -21
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +9 -9
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +1 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +16 -16
- data/lib/active_record/connection_adapters/postgresql/oid/{rails_5_1_point.rb → legacy_point.rb} +9 -16
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +28 -8
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +28 -30
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +51 -51
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +38 -36
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +37 -24
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +19 -23
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +161 -170
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +4 -4
- data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -7
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +179 -152
- data/lib/active_record/connection_adapters/schema_cache.rb +16 -7
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +3 -3
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +1 -1
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +16 -20
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +1 -8
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +28 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +187 -130
- data/lib/active_record/connection_adapters/statement_pool.rb +7 -7
- data/lib/active_record/connection_handling.rb +14 -26
- data/lib/active_record/core.rb +110 -93
- data/lib/active_record/counter_cache.rb +62 -13
- data/lib/active_record/define_callbacks.rb +20 -0
- data/lib/active_record/dynamic_matchers.rb +80 -79
- data/lib/active_record/enum.rb +8 -6
- data/lib/active_record/errors.rb +58 -15
- data/lib/active_record/explain.rb +1 -2
- data/lib/active_record/explain_registry.rb +1 -1
- data/lib/active_record/explain_subscriber.rb +7 -4
- data/lib/active_record/fixture_set/file.rb +11 -8
- data/lib/active_record/fixtures.rb +66 -53
- data/lib/active_record/gem_version.rb +3 -3
- data/lib/active_record/inheritance.rb +93 -79
- data/lib/active_record/integration.rb +7 -7
- data/lib/active_record/internal_metadata.rb +3 -16
- data/lib/active_record/legacy_yaml_adapter.rb +1 -1
- data/lib/active_record/locking/optimistic.rb +64 -56
- data/lib/active_record/locking/pessimistic.rb +10 -1
- data/lib/active_record/log_subscriber.rb +29 -29
- data/lib/active_record/migration.rb +155 -172
- data/lib/active_record/migration/command_recorder.rb +94 -94
- data/lib/active_record/migration/compatibility.rb +76 -37
- data/lib/active_record/migration/join_table.rb +6 -6
- data/lib/active_record/model_schema.rb +85 -119
- data/lib/active_record/nested_attributes.rb +200 -199
- data/lib/active_record/null_relation.rb +10 -33
- data/lib/active_record/persistence.rb +45 -38
- data/lib/active_record/query_cache.rb +4 -8
- data/lib/active_record/querying.rb +2 -3
- data/lib/active_record/railtie.rb +16 -17
- data/lib/active_record/railties/controller_runtime.rb +6 -2
- data/lib/active_record/railties/databases.rake +125 -140
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +2 -2
- data/lib/active_record/reflection.rb +79 -96
- data/lib/active_record/relation.rb +72 -115
- data/lib/active_record/relation/batches.rb +87 -58
- data/lib/active_record/relation/batches/batch_enumerator.rb +1 -1
- data/lib/active_record/relation/calculations.rb +154 -160
- data/lib/active_record/relation/delegation.rb +30 -29
- data/lib/active_record/relation/finder_methods.rb +195 -226
- data/lib/active_record/relation/merger.rb +58 -62
- data/lib/active_record/relation/predicate_builder.rb +92 -89
- data/lib/active_record/relation/predicate_builder/array_handler.rb +7 -5
- data/lib/active_record/relation/predicate_builder/association_query_handler.rb +23 -23
- data/lib/active_record/relation/predicate_builder/base_handler.rb +3 -1
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +0 -8
- data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +12 -10
- data/lib/active_record/relation/predicate_builder/range_handler.rb +0 -8
- data/lib/active_record/relation/query_attribute.rb +1 -1
- data/lib/active_record/relation/query_methods.rb +247 -295
- data/lib/active_record/relation/record_fetch_warning.rb +3 -3
- data/lib/active_record/relation/spawn_methods.rb +4 -5
- data/lib/active_record/relation/where_clause.rb +79 -65
- data/lib/active_record/relation/where_clause_factory.rb +47 -8
- data/lib/active_record/result.rb +29 -31
- data/lib/active_record/runtime_registry.rb +3 -3
- data/lib/active_record/sanitization.rb +182 -197
- data/lib/active_record/schema.rb +3 -3
- data/lib/active_record/schema_dumper.rb +14 -37
- data/lib/active_record/schema_migration.rb +3 -3
- data/lib/active_record/scoping.rb +9 -10
- data/lib/active_record/scoping/default.rb +87 -91
- data/lib/active_record/scoping/named.rb +16 -28
- data/lib/active_record/secure_token.rb +2 -2
- data/lib/active_record/statement_cache.rb +13 -15
- data/lib/active_record/store.rb +31 -32
- data/lib/active_record/suppressor.rb +2 -1
- data/lib/active_record/table_metadata.rb +9 -5
- data/lib/active_record/tasks/database_tasks.rb +72 -65
- data/lib/active_record/tasks/mysql_database_tasks.rb +75 -72
- data/lib/active_record/tasks/postgresql_database_tasks.rb +53 -48
- data/lib/active_record/tasks/sqlite_database_tasks.rb +18 -16
- data/lib/active_record/timestamp.rb +39 -25
- data/lib/active_record/touch_later.rb +1 -2
- data/lib/active_record/transactions.rb +98 -110
- data/lib/active_record/type.rb +17 -13
- data/lib/active_record/type/adapter_specific_registry.rb +46 -42
- data/lib/active_record/type/decimal_without_scale.rb +9 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +3 -3
- data/lib/active_record/type/serialized.rb +8 -8
- data/lib/active_record/type/text.rb +9 -0
- data/lib/active_record/type/time.rb +0 -1
- data/lib/active_record/type/type_map.rb +11 -15
- data/lib/active_record/type/unsigned_integer.rb +15 -0
- data/lib/active_record/type_caster.rb +2 -2
- data/lib/active_record/type_caster/connection.rb +8 -6
- data/lib/active_record/type_caster/map.rb +3 -1
- data/lib/active_record/validations.rb +4 -4
- data/lib/active_record/validations/associated.rb +1 -1
- data/lib/active_record/validations/presence.rb +2 -2
- data/lib/active_record/validations/uniqueness.rb +8 -39
- data/lib/active_record/version.rb +1 -1
- data/lib/rails/generators/active_record.rb +4 -4
- data/lib/rails/generators/active_record/migration.rb +2 -2
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -34
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -9
- metadata +22 -13
- 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 ==
|
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
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
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
|
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
|
28
|
+
autoload_at "active_record/connection_adapters/abstract/connection_pool" do
|
29
29
|
autoload :ConnectionHandler
|
30
30
|
end
|
31
31
|
|
32
|
-
autoload_under
|
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
|
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
|
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(
|
124
|
+
@version = version_string.split(".").map(&:to_i)
|
124
125
|
end
|
125
126
|
|
126
127
|
def <=>(version_string)
|
127
|
-
@version <=> version_string.split(
|
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 =
|
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 =
|
176
|
+
msg = "Cannot lease connection, "
|
168
177
|
if @owner == Thread.current
|
169
|
-
msg <<
|
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,
|
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,
|
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
|
-
|
232
|
-
|
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
|
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
|
-
|
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(
|
461
|
+
table[attribute].lower.eq(table.lower(value))
|
447
462
|
else
|
448
|
-
table[attribute].eq(
|
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
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
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
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
550
|
+
def reload_type_map
|
551
|
+
type_map.clear
|
552
|
+
initialize_type_map(type_map)
|
553
|
+
end
|
535
554
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
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
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
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
|
-
|
551
|
-
|
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
|
-
|
558
|
-
|
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
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
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
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
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
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
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
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
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
|
-
|
601
|
-
|
602
|
-
|
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
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
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
|