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
@@ -5,38 +5,11 @@ module ActiveRecord
5
5
  delegate :array, :oid, :fmod, to: :sql_type_metadata
6
6
  alias :array? :array
7
7
 
8
- def initialize(*, max_identifier_length: 63, **)
9
- super
10
- @max_identifier_length = max_identifier_length
11
- end
12
-
13
8
  def serial?
14
9
  return unless default_function
15
10
 
16
- if %r{\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z} =~ default_function
17
- sequence_name_from_parts(table_name, name, suffix) == sequence_name
18
- end
11
+ %r{\Anextval\('"?#{table_name}_#{name}_seq"?'::regclass\)\z} === default_function
19
12
  end
20
-
21
- protected
22
- attr_reader :max_identifier_length
23
-
24
- private
25
- def sequence_name_from_parts(table_name, column_name, suffix)
26
- over_length = [table_name, column_name, suffix].map(&:length).sum + 2 - max_identifier_length
27
-
28
- if over_length > 0
29
- column_name_length = [(max_identifier_length - suffix.length - 2) / 2, column_name.length].min
30
- over_length -= column_name.length - column_name_length
31
- column_name = column_name[0, column_name_length - [over_length, 0].min]
32
- end
33
-
34
- if over_length > 0
35
- table_name = table_name[0, table_name.length - over_length]
36
- end
37
-
38
- "#{table_name}_#{column_name}_#{suffix}"
39
- end
40
13
  end
41
14
  end
42
15
  end
@@ -4,21 +4,17 @@ module ActiveRecord
4
4
  module DatabaseStatements
5
5
  def explain(arel, binds = [])
6
6
  sql = "EXPLAIN #{to_sql(arel, binds)}"
7
- PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
7
+ PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", binds))
8
8
  end
9
9
 
10
- def select_value(arel, name = nil, binds = [])
11
- arel, binds = binds_from_relation arel, binds
12
- sql = to_sql(arel, binds)
13
- execute_and_clear(sql, name, binds) do |result|
10
+ def select_value(arel, name = nil, binds = []) # :nodoc:
11
+ select_result(arel, name, binds) do |result|
14
12
  result.getvalue(0, 0) if result.ntuples > 0 && result.nfields > 0
15
13
  end
16
14
  end
17
15
 
18
- def select_values(arel, name = nil, binds = [])
19
- arel, binds = binds_from_relation arel, binds
20
- sql = to_sql(arel, binds)
21
- execute_and_clear(sql, name, binds) do |result|
16
+ def select_values(arel, name = nil, binds = []) # :nodoc:
17
+ select_result(arel, name, binds) do |result|
22
18
  if result.nfields > 0
23
19
  result.column_values(0)
24
20
  else
@@ -29,8 +25,8 @@ module ActiveRecord
29
25
 
30
26
  # Executes a SELECT query and returns an array of rows. Each row is an
31
27
  # array of field values.
32
- def select_rows(sql, name = nil, binds = [])
33
- execute_and_clear(sql, name, binds) do |result|
28
+ def select_rows(arel, name = nil, binds = []) # :nodoc:
29
+ select_result(arel, name, binds) do |result|
34
30
  result.values
35
31
  end
36
32
  end
@@ -74,9 +70,9 @@ module ActiveRecord
74
70
  # (2) $12.345.678,12
75
71
  case data
76
72
  when /^-?\D+[\d,]+\.\d{2}$/ # (1)
77
- data.gsub!(/[^-\d.]/, '')
73
+ data.gsub!(/[^-\d.]/, "")
78
74
  when /^-?\D+[\d.]+,\d{2}$/ # (2)
79
- data.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
75
+ data.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
80
76
  end
81
77
  end
82
78
  end
@@ -85,21 +81,25 @@ module ActiveRecord
85
81
  # Queries the database and returns the results in an Array-like object
86
82
  def query(sql, name = nil) #:nodoc:
87
83
  log(sql, name) do
88
- result_as_array @connection.async_exec(sql)
84
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
85
+ result_as_array @connection.async_exec(sql)
86
+ end
89
87
  end
90
88
  end
91
89
 
92
- # Executes an SQL statement, returning a PGresult object on success
93
- # or raising a PGError exception otherwise.
94
- # Note: the PGresult object is manually memory managed; if you don't
95
- # need it specifically, you many want consider the exec_query wrapper.
90
+ # Executes an SQL statement, returning a PG::Result object on success
91
+ # or raising a PG::Error exception otherwise.
92
+ # Note: the PG::Result object is manually memory managed; if you don't
93
+ # need it specifically, you may want consider the <tt>exec_query</tt> wrapper.
96
94
  def execute(sql, name = nil)
97
95
  log(sql, name) do
98
- @connection.async_exec(sql)
96
+ ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
97
+ @connection.async_exec(sql)
98
+ end
99
99
  end
100
100
  end
101
101
 
102
- def exec_query(sql, name = 'SQL', binds = [], prepare: false)
102
+ def exec_query(sql, name = "SQL", binds = [], prepare: false)
103
103
  execute_and_clear(sql, name, binds, prepare: prepare) do |result|
104
104
  types = {}
105
105
  fields = result.fields
@@ -112,8 +112,8 @@ module ActiveRecord
112
112
  end
113
113
  end
114
114
 
115
- def exec_delete(sql, name = 'SQL', binds = [])
116
- execute_and_clear(sql, name, binds) {|result| result.cmd_tuples }
115
+ def exec_delete(sql, name = nil, binds = [])
116
+ execute_and_clear(sql, name, binds) { |result| result.cmd_tuples }
117
117
  end
118
118
  alias :exec_update :exec_delete
119
119
 
@@ -124,26 +124,29 @@ module ActiveRecord
124
124
  pk = primary_key(table_ref) if table_ref
125
125
  end
126
126
 
127
- pk = suppress_composite_primary_key(pk)
128
-
129
- if pk && use_insert_returning?
127
+ if pk = suppress_composite_primary_key(pk)
130
128
  sql = "#{sql} RETURNING #{quote_column_name(pk)}"
131
129
  end
132
130
 
133
131
  super
134
132
  end
133
+ private :sql_for_insert
135
134
 
136
- def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
137
- val = exec_query(sql, name, binds)
138
- if !use_insert_returning? && pk
135
+ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
136
+ if use_insert_returning? || pk == false
137
+ super
138
+ else
139
+ result = exec_query(sql, name, binds)
139
140
  unless sequence_name
140
141
  table_ref = extract_table_ref_from_insert_sql(sql)
141
- sequence_name = default_sequence_name(table_ref, pk)
142
- return val unless sequence_name
142
+ if table_ref
143
+ pk = primary_key(table_ref) if pk.nil?
144
+ pk = suppress_composite_primary_key(pk)
145
+ sequence_name = default_sequence_name(table_ref, pk)
146
+ end
147
+ return result unless sequence_name
143
148
  end
144
149
  last_insert_id_result(sequence_name)
145
- else
146
- val
147
150
  end
148
151
  end
149
152
 
@@ -169,9 +172,17 @@ module ActiveRecord
169
172
 
170
173
  private
171
174
 
172
- def suppress_composite_primary_key(pk)
173
- pk unless pk.is_a?(Array)
174
- end
175
+ def suppress_composite_primary_key(pk)
176
+ pk unless pk.is_a?(Array)
177
+ end
178
+
179
+ def select_result(arel, name, binds)
180
+ arel, binds = binds_from_relation(arel, binds)
181
+ sql = to_sql(arel, binds)
182
+ execute_and_clear(sql, name, binds) do |result|
183
+ yield result
184
+ end
185
+ end
175
186
  end
176
187
  end
177
188
  end
@@ -26,12 +26,12 @@ module ActiveRecord
26
26
  pp = []
27
27
 
28
28
  pp << header.center(width).rstrip
29
- pp << '-' * width
29
+ pp << "-" * width
30
30
 
31
- pp += lines.map {|line| " #{line}"}
31
+ pp += lines.map { |line| " #{line}" }
32
32
 
33
33
  nrows = result.rows.length
34
- rows_label = nrows == 1 ? 'row' : 'rows'
34
+ rows_label = nrows == 1 ? "row" : "rows"
35
35
  pp << "(#{nrows} #{rows_label})"
36
36
 
37
37
  pp.join("\n") + "\n"
@@ -1,25 +1,26 @@
1
- require 'active_record/connection_adapters/postgresql/oid/array'
2
- require 'active_record/connection_adapters/postgresql/oid/bit'
3
- require 'active_record/connection_adapters/postgresql/oid/bit_varying'
4
- require 'active_record/connection_adapters/postgresql/oid/bytea'
5
- require 'active_record/connection_adapters/postgresql/oid/cidr'
6
- require 'active_record/connection_adapters/postgresql/oid/date_time'
7
- require 'active_record/connection_adapters/postgresql/oid/decimal'
8
- require 'active_record/connection_adapters/postgresql/oid/enum'
9
- require 'active_record/connection_adapters/postgresql/oid/hstore'
10
- require 'active_record/connection_adapters/postgresql/oid/inet'
11
- require 'active_record/connection_adapters/postgresql/oid/json'
12
- require 'active_record/connection_adapters/postgresql/oid/jsonb'
13
- require 'active_record/connection_adapters/postgresql/oid/money'
14
- require 'active_record/connection_adapters/postgresql/oid/point'
15
- require 'active_record/connection_adapters/postgresql/oid/rails_5_1_point'
16
- require 'active_record/connection_adapters/postgresql/oid/range'
17
- require 'active_record/connection_adapters/postgresql/oid/specialized_string'
18
- require 'active_record/connection_adapters/postgresql/oid/uuid'
19
- require 'active_record/connection_adapters/postgresql/oid/vector'
20
- require 'active_record/connection_adapters/postgresql/oid/xml'
1
+ require "active_record/connection_adapters/postgresql/oid/array"
2
+ require "active_record/connection_adapters/postgresql/oid/bit"
3
+ require "active_record/connection_adapters/postgresql/oid/bit_varying"
4
+ require "active_record/connection_adapters/postgresql/oid/bytea"
5
+ require "active_record/connection_adapters/postgresql/oid/cidr"
6
+ require "active_record/connection_adapters/postgresql/oid/date_time"
7
+ require "active_record/connection_adapters/postgresql/oid/decimal"
8
+ require "active_record/connection_adapters/postgresql/oid/enum"
9
+ require "active_record/connection_adapters/postgresql/oid/hstore"
10
+ require "active_record/connection_adapters/postgresql/oid/inet"
11
+ require "active_record/connection_adapters/postgresql/oid/json"
12
+ require "active_record/connection_adapters/postgresql/oid/jsonb"
13
+ require "active_record/connection_adapters/postgresql/oid/money"
14
+ require "active_record/connection_adapters/postgresql/oid/oid"
15
+ require "active_record/connection_adapters/postgresql/oid/point"
16
+ require "active_record/connection_adapters/postgresql/oid/legacy_point"
17
+ require "active_record/connection_adapters/postgresql/oid/range"
18
+ require "active_record/connection_adapters/postgresql/oid/specialized_string"
19
+ require "active_record/connection_adapters/postgresql/oid/uuid"
20
+ require "active_record/connection_adapters/postgresql/oid/vector"
21
+ require "active_record/connection_adapters/postgresql/oid/xml"
21
22
 
22
- require 'active_record/connection_adapters/postgresql/oid/type_map_initializer'
23
+ require "active_record/connection_adapters/postgresql/oid/type_map_initializer"
23
24
 
24
25
  module ActiveRecord
25
26
  module ConnectionAdapters
@@ -8,9 +8,9 @@ module ActiveRecord
8
8
  Data = Struct.new(:encoder, :values) # :nodoc:
9
9
 
10
10
  attr_reader :subtype, :delimiter
11
- delegate :type, :user_input_in_time_zone, :limit, to: :subtype
11
+ delegate :type, :user_input_in_time_zone, :limit, :precision, :scale, to: :subtype
12
12
 
13
- def initialize(subtype, delimiter = ',')
13
+ def initialize(subtype, delimiter = ",")
14
14
  @subtype = subtype
15
15
  @delimiter = delimiter
16
16
 
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
  when ::String
24
24
  type_cast_array(@pg_decoder.decode(value), :deserialize)
25
25
  when Data
26
- type_cast_array(value.values, :deserialize)
26
+ deserialize(value.values)
27
27
  else
28
28
  super
29
29
  end
@@ -66,13 +66,13 @@ module ActiveRecord
66
66
 
67
67
  private
68
68
 
69
- def type_cast_array(value, method)
70
- if value.is_a?(::Array)
71
- value.map { |item| type_cast_array(item, method) }
72
- else
73
- @subtype.public_send(method, value)
69
+ def type_cast_array(value, method)
70
+ if value.is_a?(::Array)
71
+ value.map { |item| type_cast_array(item, method) }
72
+ else
73
+ @subtype.public_send(method, value)
74
+ end
74
75
  end
75
- end
76
76
  end
77
77
  end
78
78
  end
@@ -34,16 +34,18 @@ module ActiveRecord
34
34
  end
35
35
 
36
36
  def binary?
37
- /\A[01]*\Z/ === value
37
+ /\A[01]*\Z/.match?(value)
38
38
  end
39
39
 
40
40
  def hex?
41
- /\A[0-9A-F]*\Z/i === value
41
+ /\A[0-9A-F]*\Z/i.match?(value)
42
42
  end
43
43
 
44
+ # TODO Change this to private once we've dropped Ruby 2.2 support.
45
+ # Workaround for Ruby 2.2 "private attribute?" warning.
44
46
  protected
45
47
 
46
- attr_reader :value
48
+ attr_reader :value
47
49
  end
48
50
  end
49
51
  end
@@ -6,7 +6,7 @@ module ActiveRecord
6
6
  def deserialize(value)
7
7
  return if value.nil?
8
8
  return value.to_s if value.is_a?(Type::Binary::Data)
9
- PG::Connection.unescape_bytea(super)
9
+ PGconn.unescape_bytea(super)
10
10
  end
11
11
  end
12
12
  end
@@ -1,4 +1,4 @@
1
- require 'ipaddr'
1
+ require "ipaddr"
2
2
 
3
3
  module ActiveRecord
4
4
  module ConnectionAdapters
@@ -5,8 +5,8 @@ module ActiveRecord
5
5
  class DateTime < Type::DateTime # :nodoc:
6
6
  def cast_value(value)
7
7
  case value
8
- when 'infinity' then ::Float::INFINITY
9
- when '-infinity' then -::Float::INFINITY
8
+ when "infinity" then ::Float::INFINITY
9
+ when "-infinity" then -::Float::INFINITY
10
10
  when / BC$/
11
11
  astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
12
12
  super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
@@ -9,9 +9,9 @@ module ActiveRecord
9
9
 
10
10
  private
11
11
 
12
- def cast_value(value)
13
- value.to_s
14
- end
12
+ def cast_value(value)
13
+ value.to_s
14
+ end
15
15
  end
16
16
  end
17
17
  end
@@ -12,8 +12,8 @@ module ActiveRecord
12
12
  def deserialize(value)
13
13
  if value.is_a?(::String)
14
14
  ::Hash[value.scan(HstorePair).map { |k, v|
15
- v = v.upcase == 'NULL' ? nil : v.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
16
- k = k.gsub(/\A"(.*)"\Z/m,'\1').gsub(/\\(.)/, '\1')
15
+ v = v.upcase == "NULL" ? nil : v.gsub(/\A"(.*)"\Z/m, '\1').gsub(/\\(.)/, '\1')
16
+ k = k.gsub(/\A"(.*)"\Z/m, '\1').gsub(/\\(.)/, '\1')
17
17
  [k, v]
18
18
  }]
19
19
  else
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
 
24
24
  def serialize(value)
25
25
  if value.is_a?(::Hash)
26
- value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ')
26
+ value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(", ")
27
27
  elsif value.respond_to?(:to_unsafe_h)
28
28
  serialize(value.to_unsafe_h)
29
29
  else
@@ -45,23 +45,23 @@ module ActiveRecord
45
45
 
46
46
  private
47
47
 
48
- HstorePair = begin
49
- quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
50
- unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
51
- /(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
52
- end
48
+ HstorePair = begin
49
+ quoted_string = /"[^"\\]*(?:\\.[^"\\]*)*"/
50
+ unquoted_string = /(?:\\.|[^\s,])[^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/
51
+ /(#{quoted_string}|#{unquoted_string})\s*=>\s*(#{quoted_string}|#{unquoted_string})/
52
+ end
53
53
 
54
- def escape_hstore(value)
55
- if value.nil?
56
- 'NULL'
57
- else
58
- if value == ""
59
- '""'
54
+ def escape_hstore(value)
55
+ if value.nil?
56
+ "NULL"
60
57
  else
61
- '"%s"' % value.to_s.gsub(/(["\\])/, '\\\\\1')
58
+ if value == ""
59
+ '""'
60
+ else
61
+ '"%s"' % value.to_s.gsub(/(["\\])/, '\\\\\1')
62
+ end
62
63
  end
63
64
  end
64
- end
65
65
  end
66
66
  end
67
67
  end
@@ -1,10 +1,8 @@
1
1
  module ActiveRecord
2
- Point = Struct.new(:x, :y)
3
-
4
2
  module ConnectionAdapters
5
3
  module PostgreSQL
6
4
  module OID # :nodoc:
7
- class Rails51Point < Type::Value # :nodoc:
5
+ class LegacyPoint < Type::Value # :nodoc:
8
6
  include Type::Helpers::Mutable
9
7
 
10
8
  def type
@@ -14,21 +12,20 @@ module ActiveRecord
14
12
  def cast(value)
15
13
  case value
16
14
  when ::String
17
- if value[0] == '(' && value[-1] == ')'
15
+ if value[0] == "(" && value[-1] == ")"
18
16
  value = value[1...-1]
19
17
  end
20
- x, y = value.split(",")
21
- build_point(x, y)
18
+ cast(value.split(","))
22
19
  when ::Array
23
- build_point(*value)
20
+ value.map { |v| Float(v) }
24
21
  else
25
22
  value
26
23
  end
27
24
  end
28
25
 
29
26
  def serialize(value)
30
- if value.is_a?(ActiveRecord::Point)
31
- "(#{number_for_point(value.x)},#{number_for_point(value.y)})"
27
+ if value.is_a?(::Array)
28
+ "(#{number_for_point(value[0])},#{number_for_point(value[1])})"
32
29
  else
33
30
  super
34
31
  end
@@ -36,13 +33,9 @@ module ActiveRecord
36
33
 
37
34
  private
38
35
 
39
- def number_for_point(number)
40
- number.to_s.gsub(/\.0$/, '')
41
- end
42
-
43
- def build_point(x, y)
44
- ActiveRecord::Point.new(Float(x), Float(y))
45
- end
36
+ def number_for_point(number)
37
+ number.to_s.gsub(/\.0$/, "")
38
+ end
46
39
  end
47
40
  end
48
41
  end