activerecord 4.1.8 → 4.2.11.3

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 (186) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1165 -1591
  3. data/README.rdoc +15 -10
  4. data/lib/active_record/aggregations.rb +15 -8
  5. data/lib/active_record/association_relation.rb +13 -0
  6. data/lib/active_record/associations/alias_tracker.rb +3 -12
  7. data/lib/active_record/associations/association.rb +16 -4
  8. data/lib/active_record/associations/association_scope.rb +84 -43
  9. data/lib/active_record/associations/belongs_to_association.rb +28 -10
  10. data/lib/active_record/associations/builder/association.rb +16 -5
  11. data/lib/active_record/associations/builder/belongs_to.rb +7 -29
  12. data/lib/active_record/associations/builder/collection_association.rb +5 -1
  13. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +9 -14
  14. data/lib/active_record/associations/builder/has_many.rb +1 -1
  15. data/lib/active_record/associations/builder/has_one.rb +2 -2
  16. data/lib/active_record/associations/builder/singular_association.rb +8 -1
  17. data/lib/active_record/associations/collection_association.rb +87 -30
  18. data/lib/active_record/associations/collection_proxy.rb +33 -35
  19. data/lib/active_record/associations/foreign_association.rb +11 -0
  20. data/lib/active_record/associations/has_many_association.rb +83 -22
  21. data/lib/active_record/associations/has_many_through_association.rb +49 -26
  22. data/lib/active_record/associations/has_one_association.rb +1 -1
  23. data/lib/active_record/associations/join_dependency/join_association.rb +25 -15
  24. data/lib/active_record/associations/join_dependency/join_part.rb +0 -1
  25. data/lib/active_record/associations/join_dependency.rb +26 -12
  26. data/lib/active_record/associations/preloader/association.rb +14 -10
  27. data/lib/active_record/associations/preloader/through_association.rb +4 -3
  28. data/lib/active_record/associations/preloader.rb +37 -26
  29. data/lib/active_record/associations/singular_association.rb +17 -2
  30. data/lib/active_record/associations/through_association.rb +16 -12
  31. data/lib/active_record/associations.rb +158 -49
  32. data/lib/active_record/attribute.rb +163 -0
  33. data/lib/active_record/attribute_assignment.rb +20 -12
  34. data/lib/active_record/attribute_decorators.rb +66 -0
  35. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
  36. data/lib/active_record/attribute_methods/dirty.rb +107 -43
  37. data/lib/active_record/attribute_methods/primary_key.rb +7 -8
  38. data/lib/active_record/attribute_methods/query.rb +1 -1
  39. data/lib/active_record/attribute_methods/read.rb +22 -59
  40. data/lib/active_record/attribute_methods/serialization.rb +16 -150
  41. data/lib/active_record/attribute_methods/time_zone_conversion.rb +38 -28
  42. data/lib/active_record/attribute_methods/write.rb +9 -24
  43. data/lib/active_record/attribute_methods.rb +57 -95
  44. data/lib/active_record/attribute_set/builder.rb +106 -0
  45. data/lib/active_record/attribute_set.rb +81 -0
  46. data/lib/active_record/attributes.rb +147 -0
  47. data/lib/active_record/autosave_association.rb +30 -12
  48. data/lib/active_record/base.rb +13 -24
  49. data/lib/active_record/callbacks.rb +6 -6
  50. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +85 -53
  51. data/lib/active_record/connection_adapters/abstract/database_statements.rb +52 -50
  52. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  53. data/lib/active_record/connection_adapters/abstract/quoting.rb +60 -60
  54. data/lib/active_record/connection_adapters/abstract/savepoints.rb +1 -1
  55. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +39 -4
  56. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +139 -57
  57. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -34
  58. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +271 -74
  59. data/lib/active_record/connection_adapters/abstract/transaction.rb +125 -118
  60. data/lib/active_record/connection_adapters/abstract_adapter.rb +177 -60
  61. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +295 -141
  62. data/lib/active_record/connection_adapters/column.rb +29 -240
  63. data/lib/active_record/connection_adapters/connection_specification.rb +15 -24
  64. data/lib/active_record/connection_adapters/mysql2_adapter.rb +17 -33
  65. data/lib/active_record/connection_adapters/mysql_adapter.rb +68 -145
  66. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +15 -27
  67. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  68. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -25
  69. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +100 -0
  70. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
  73. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  74. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +36 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
  78. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  79. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  80. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  81. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  82. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  83. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +19 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid.rb +29 -385
  95. data/lib/active_record/connection_adapters/postgresql/quoting.rb +46 -136
  96. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +4 -4
  97. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  98. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +134 -43
  99. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  100. data/lib/active_record/connection_adapters/postgresql_adapter.rb +224 -477
  101. data/lib/active_record/connection_adapters/schema_cache.rb +14 -28
  102. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +61 -75
  103. data/lib/active_record/connection_handling.rb +1 -1
  104. data/lib/active_record/core.rb +163 -40
  105. data/lib/active_record/counter_cache.rb +60 -6
  106. data/lib/active_record/enum.rb +10 -12
  107. data/lib/active_record/errors.rb +53 -30
  108. data/lib/active_record/explain.rb +1 -1
  109. data/lib/active_record/explain_subscriber.rb +1 -1
  110. data/lib/active_record/fixtures.rb +62 -74
  111. data/lib/active_record/gem_version.rb +4 -4
  112. data/lib/active_record/inheritance.rb +35 -10
  113. data/lib/active_record/integration.rb +4 -4
  114. data/lib/active_record/legacy_yaml_adapter.rb +30 -0
  115. data/lib/active_record/locking/optimistic.rb +46 -26
  116. data/lib/active_record/migration/command_recorder.rb +19 -2
  117. data/lib/active_record/migration/join_table.rb +1 -1
  118. data/lib/active_record/migration.rb +79 -47
  119. data/lib/active_record/model_schema.rb +52 -58
  120. data/lib/active_record/nested_attributes.rb +18 -8
  121. data/lib/active_record/no_touching.rb +1 -1
  122. data/lib/active_record/persistence.rb +48 -27
  123. data/lib/active_record/query_cache.rb +3 -3
  124. data/lib/active_record/querying.rb +10 -7
  125. data/lib/active_record/railtie.rb +19 -14
  126. data/lib/active_record/railties/databases.rake +55 -56
  127. data/lib/active_record/readonly_attributes.rb +0 -1
  128. data/lib/active_record/reflection.rb +281 -117
  129. data/lib/active_record/relation/batches.rb +0 -1
  130. data/lib/active_record/relation/calculations.rb +41 -37
  131. data/lib/active_record/relation/delegation.rb +1 -1
  132. data/lib/active_record/relation/finder_methods.rb +71 -48
  133. data/lib/active_record/relation/merger.rb +39 -29
  134. data/lib/active_record/relation/predicate_builder/array_handler.rb +32 -13
  135. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  136. data/lib/active_record/relation/predicate_builder.rb +42 -12
  137. data/lib/active_record/relation/query_methods.rb +130 -73
  138. data/lib/active_record/relation/spawn_methods.rb +10 -3
  139. data/lib/active_record/relation.rb +57 -25
  140. data/lib/active_record/result.rb +18 -7
  141. data/lib/active_record/sanitization.rb +12 -2
  142. data/lib/active_record/schema.rb +0 -1
  143. data/lib/active_record/schema_dumper.rb +59 -28
  144. data/lib/active_record/schema_migration.rb +5 -4
  145. data/lib/active_record/scoping/default.rb +6 -4
  146. data/lib/active_record/scoping/named.rb +4 -0
  147. data/lib/active_record/serializers/xml_serializer.rb +3 -7
  148. data/lib/active_record/statement_cache.rb +95 -10
  149. data/lib/active_record/store.rb +5 -5
  150. data/lib/active_record/tasks/database_tasks.rb +61 -8
  151. data/lib/active_record/tasks/mysql_database_tasks.rb +32 -17
  152. data/lib/active_record/tasks/postgresql_database_tasks.rb +20 -9
  153. data/lib/active_record/timestamp.rb +9 -7
  154. data/lib/active_record/transactions.rb +54 -28
  155. data/lib/active_record/type/big_integer.rb +13 -0
  156. data/lib/active_record/type/binary.rb +50 -0
  157. data/lib/active_record/type/boolean.rb +31 -0
  158. data/lib/active_record/type/date.rb +50 -0
  159. data/lib/active_record/type/date_time.rb +54 -0
  160. data/lib/active_record/type/decimal.rb +64 -0
  161. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  162. data/lib/active_record/type/decorator.rb +14 -0
  163. data/lib/active_record/type/float.rb +19 -0
  164. data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
  165. data/lib/active_record/type/integer.rb +59 -0
  166. data/lib/active_record/type/mutable.rb +16 -0
  167. data/lib/active_record/type/numeric.rb +36 -0
  168. data/lib/active_record/type/serialized.rb +62 -0
  169. data/lib/active_record/type/string.rb +40 -0
  170. data/lib/active_record/type/text.rb +11 -0
  171. data/lib/active_record/type/time.rb +26 -0
  172. data/lib/active_record/type/time_value.rb +38 -0
  173. data/lib/active_record/type/type_map.rb +64 -0
  174. data/lib/active_record/type/unsigned_integer.rb +15 -0
  175. data/lib/active_record/type/value.rb +110 -0
  176. data/lib/active_record/type.rb +23 -0
  177. data/lib/active_record/validations/associated.rb +5 -3
  178. data/lib/active_record/validations/presence.rb +5 -3
  179. data/lib/active_record/validations/uniqueness.rb +24 -20
  180. data/lib/active_record/validations.rb +25 -19
  181. data/lib/active_record.rb +5 -0
  182. data/lib/rails/generators/active_record/migration/migration_generator.rb +8 -4
  183. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +1 -1
  184. data/lib/rails/generators/active_record/model/templates/model.rb +1 -1
  185. metadata +66 -11
  186. data/lib/active_record/connection_adapters/postgresql/cast.rb +0 -168
@@ -13,101 +13,36 @@ module ActiveRecord
13
13
  ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
14
14
  end
15
15
 
16
- attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale, :default_function
17
- attr_accessor :primary, :coder
16
+ attr_reader :name, :cast_type, :null, :sql_type, :default, :default_function
18
17
 
19
- alias :encoded? :coder
18
+ delegate :type, :precision, :scale, :limit, :klass, :accessor,
19
+ :text?, :number?, :binary?, :changed?,
20
+ :type_cast_from_user, :type_cast_from_database, :type_cast_for_database,
21
+ :type_cast_for_schema,
22
+ to: :cast_type
20
23
 
21
24
  # Instantiates a new column in the table.
22
25
  #
23
26
  # +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int(11)</tt>.
24
27
  # +default+ is the type-casted default value, such as +new+ in <tt>sales_stage varchar(20) default 'new'</tt>.
28
+ # +cast_type+ is the object used for type casting and type information.
25
29
  # +sql_type+ is used to extract the column's length, if necessary. For example +60+ in
26
30
  # <tt>company_name varchar(60)</tt>.
27
31
  # It will be mapped to one of the standard Rails SQL types in the <tt>type</tt> attribute.
28
32
  # +null+ determines if this column allows +NULL+ values.
29
- def initialize(name, default, sql_type = nil, null = true)
30
- @name = name
33
+ def initialize(name, default, cast_type, sql_type = nil, null = true)
34
+ @name = name.freeze
35
+ @cast_type = cast_type
31
36
  @sql_type = sql_type
32
37
  @null = null
33
- @limit = extract_limit(sql_type)
34
- @precision = extract_precision(sql_type)
35
- @scale = extract_scale(sql_type)
36
- @type = simplified_type(sql_type)
37
- @default = extract_default(default)
38
+ @default = default
38
39
  @default_function = nil
39
- @primary = nil
40
- @coder = nil
41
- end
42
-
43
- # Returns +true+ if the column is either of type string or text.
44
- def text?
45
- type == :string || type == :text
46
- end
47
-
48
- # Returns +true+ if the column is either of type integer, float or decimal.
49
- def number?
50
- type == :integer || type == :float || type == :decimal
51
40
  end
52
41
 
53
42
  def has_default?
54
43
  !default.nil?
55
44
  end
56
45
 
57
- # Returns the Ruby class that corresponds to the abstract data type.
58
- def klass
59
- case type
60
- when :integer then Fixnum
61
- when :float then Float
62
- when :decimal then BigDecimal
63
- when :datetime, :timestamp, :time then Time
64
- when :date then Date
65
- when :text, :string, :binary then String
66
- when :boolean then Object
67
- end
68
- end
69
-
70
- def binary?
71
- type == :binary
72
- end
73
-
74
- # Casts a Ruby value to something appropriate for writing to the database.
75
- def type_cast_for_write(value)
76
- return value unless number?
77
-
78
- case value
79
- when FalseClass
80
- 0
81
- when TrueClass
82
- 1
83
- when String
84
- value.presence
85
- else
86
- value
87
- end
88
- end
89
-
90
- # Casts value (which is a String) to an appropriate instance.
91
- def type_cast(value)
92
- return nil if value.nil?
93
- return coder.load(value) if encoded?
94
-
95
- klass = self.class
96
-
97
- case type
98
- when :string, :text then value
99
- when :integer then klass.value_to_integer(value)
100
- when :float then value.to_f
101
- when :decimal then klass.value_to_decimal(value)
102
- when :datetime, :timestamp then klass.string_to_time(value)
103
- when :time then klass.string_to_dummy_time(value)
104
- when :date then klass.value_to_date(value)
105
- when :binary then klass.binary_to_string(value)
106
- when :boolean then klass.value_to_boolean(value)
107
- else value
108
- end
109
- end
110
-
111
46
  # Returns the human name of the column name.
112
47
  #
113
48
  # ===== Examples
@@ -116,177 +51,31 @@ module ActiveRecord
116
51
  Base.human_attribute_name(@name)
117
52
  end
118
53
 
119
- def extract_default(default)
120
- type_cast(default)
121
- end
122
-
123
- class << self
124
- # Used to convert from BLOBs to Strings
125
- def binary_to_string(value)
126
- value
127
- end
128
-
129
- def value_to_date(value)
130
- if value.is_a?(String)
131
- return nil if value.empty?
132
- fast_string_to_date(value) || fallback_string_to_date(value)
133
- elsif value.respond_to?(:to_date)
134
- value.to_date
135
- else
136
- value
137
- end
138
- end
139
-
140
- def string_to_time(string)
141
- return string unless string.is_a?(String)
142
- return nil if string.empty?
143
-
144
- fast_string_to_time(string) || fallback_string_to_time(string)
145
- end
146
-
147
- def string_to_dummy_time(string)
148
- return string unless string.is_a?(String)
149
- return nil if string.empty?
150
-
151
- dummy_time_string = "2000-01-01 #{string}"
152
-
153
- fast_string_to_time(dummy_time_string) || begin
154
- time_hash = Date._parse(dummy_time_string)
155
- return nil if time_hash[:hour].nil?
156
- new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
157
- end
158
- end
159
-
160
- # convert something to a boolean
161
- def value_to_boolean(value)
162
- if value.is_a?(String) && value.empty?
163
- nil
164
- else
165
- TRUE_VALUES.include?(value)
166
- end
167
- end
168
-
169
- # Used to convert values to integer.
170
- # handle the case when an integer column is used to store boolean values
171
- def value_to_integer(value)
172
- case value
173
- when TrueClass, FalseClass
174
- value ? 1 : 0
175
- else
176
- value.to_i rescue nil
177
- end
54
+ def with_type(type)
55
+ dup.tap do |clone|
56
+ clone.instance_variable_set('@cast_type', type)
178
57
  end
58
+ end
179
59
 
180
- # convert something to a BigDecimal
181
- def value_to_decimal(value)
182
- # Using .class is faster than .is_a? and
183
- # subclasses of BigDecimal will be handled
184
- # in the else clause
185
- if value.class == BigDecimal
186
- value
187
- elsif value.respond_to?(:to_d)
188
- value.to_d
189
- else
190
- value.to_s.to_d
191
- end
192
- end
193
-
194
- protected
195
- # '0.123456' -> 123456
196
- # '1.123456' -> 123456
197
- def microseconds(time)
198
- time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
199
- end
200
-
201
- def new_date(year, mon, mday)
202
- if year && year != 0
203
- Date.new(year, mon, mday) rescue nil
204
- end
205
- end
206
-
207
- def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
208
- # Treat 0000-00-00 00:00:00 as nil.
209
- return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
210
-
211
- if offset
212
- time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
213
- return nil unless time
214
-
215
- time -= offset
216
- Base.default_timezone == :utc ? time : time.getlocal
217
- else
218
- Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
219
- end
220
- end
221
-
222
- def fast_string_to_date(string)
223
- if string =~ Format::ISO_DATE
224
- new_date $1.to_i, $2.to_i, $3.to_i
225
- end
226
- end
227
-
228
- # Doesn't handle time zones.
229
- def fast_string_to_time(string)
230
- if string =~ Format::ISO_DATETIME
231
- microsec = ($7.to_r * 1_000_000).to_i
232
- new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
233
- end
234
- end
235
-
236
- def fallback_string_to_date(string)
237
- new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
238
- end
239
-
240
- def fallback_string_to_time(string)
241
- time_hash = Date._parse(string)
242
- time_hash[:sec_fraction] = microseconds(time_hash)
60
+ def ==(other)
61
+ other.name == name &&
62
+ other.default == default &&
63
+ other.cast_type == cast_type &&
64
+ other.sql_type == sql_type &&
65
+ other.null == null &&
66
+ other.default_function == default_function
67
+ end
68
+ alias :eql? :==
243
69
 
244
- new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
245
- end
70
+ def hash
71
+ attributes_for_hash.hash
246
72
  end
247
73
 
248
74
  private
249
- def extract_limit(sql_type)
250
- $1.to_i if sql_type =~ /\((.*)\)/
251
- end
252
75
 
253
- def extract_precision(sql_type)
254
- $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
255
- end
256
-
257
- def extract_scale(sql_type)
258
- case sql_type
259
- when /^(numeric|decimal|number)\((\d+)\)/i then 0
260
- when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
261
- end
262
- end
263
-
264
- def simplified_type(field_type)
265
- case field_type
266
- when /int/i
267
- :integer
268
- when /float|double/i
269
- :float
270
- when /decimal|numeric|number/i
271
- extract_scale(field_type) == 0 ? :integer : :decimal
272
- when /datetime/i
273
- :datetime
274
- when /timestamp/i
275
- :timestamp
276
- when /time/i
277
- :time
278
- when /date/i
279
- :date
280
- when /clob/i, /text/i
281
- :text
282
- when /blob/i, /binary/i
283
- :binary
284
- when /char/i
285
- :string
286
- when /boolean/i
287
- :boolean
288
- end
289
- end
76
+ def attributes_for_hash
77
+ [self.class, name, default, cast_type, sql_type, null, default_function]
78
+ end
290
79
  end
291
80
  end
292
81
  # :startdoc:
@@ -1,4 +1,5 @@
1
1
  require 'uri'
2
+ require 'active_support/core_ext/string/filters'
2
3
 
3
4
  module ActiveRecord
4
5
  module ConnectionAdapters
@@ -32,8 +33,8 @@ module ActiveRecord
32
33
  # }
33
34
  def initialize(url)
34
35
  raise "Database URL cannot be empty" if url.blank?
35
- @uri = URI.parse(url)
36
- @adapter = @uri.scheme.gsub('-', '_')
36
+ @uri = uri_parser.parse(url)
37
+ @adapter = @uri.scheme.tr('-', '_')
37
38
  @adapter = "postgresql" if @adapter == "postgres"
38
39
 
39
40
  if @uri.opaque
@@ -41,7 +42,6 @@ module ActiveRecord
41
42
  else
42
43
  @query = @uri.query
43
44
  end
44
- @authority = url =~ %r{\A[^:]*://}
45
45
  end
46
46
 
47
47
  # Converts the given URL to a full connection hash.
@@ -63,8 +63,8 @@ module ActiveRecord
63
63
 
64
64
  # Converts the query parameters of the URI into a hash.
65
65
  #
66
- # "localhost?pool=5&reap_frequency=2"
67
- # # => { "pool" => "5", "reap_frequency" => "2" }
66
+ # "localhost?pool=5&reaping_frequency=2"
67
+ # # => { "pool" => "5", "reaping_frequency" => "2" }
68
68
  #
69
69
  # returns empty hash if no query present.
70
70
  #
@@ -91,21 +91,8 @@ module ActiveRecord
91
91
  end
92
92
 
93
93
  # Returns name of the database.
94
- # Sqlite3's handling of a leading slash is in transition as of
95
- # Rails 4.1.
96
94
  def database_from_path
97
- if @authority && @adapter == 'sqlite3'
98
- # 'sqlite3:///foo' is relative, for backwards compatibility.
99
-
100
- database_name = uri.path.sub(%r{^/}, "")
101
-
102
- msg = "Paths in SQLite3 database URLs of the form `sqlite3:///path` will be treated as absolute in Rails 4.2. " \
103
- "Please switch to `sqlite3:#{database_name}`."
104
- ActiveSupport::Deprecation.warn(msg)
105
-
106
- database_name
107
-
108
- elsif @adapter == 'sqlite3'
95
+ if @adapter == 'sqlite3'
109
96
  # 'sqlite3:/foo' is absolute, because that makes sense. The
110
97
  # corresponding relative version, 'sqlite3:foo', is handled
111
98
  # elsewhere, as an "opaque".
@@ -174,7 +161,7 @@ module ActiveRecord
174
161
  # config = { "production" => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" } }
175
162
  # spec = Resolver.new(config).spec(:production)
176
163
  # spec.adapter_method
177
- # # => "sqlite3"
164
+ # # => "sqlite3_connection"
178
165
  # spec.config
179
166
  # # => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" }
180
167
  #
@@ -235,15 +222,19 @@ module ActiveRecord
235
222
  # this ambiguous behaviour and in the future this function
236
223
  # can be removed in favor of resolve_url_connection.
237
224
  if configurations.key?(spec) || spec !~ /:/
238
- ActiveSupport::Deprecation.warn "Passing a string to ActiveRecord::Base.establish_connection " \
239
- "for a configuration lookup is deprecated, please pass a symbol (#{spec.to_sym.inspect}) instead"
225
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
226
+ Passing a string to ActiveRecord::Base.establish_connection for a
227
+ configuration lookup is deprecated, please pass a symbol
228
+ (#{spec.to_sym.inspect}) instead.
229
+ MSG
230
+
240
231
  resolve_symbol_connection(spec)
241
232
  else
242
233
  resolve_url_connection(spec)
243
234
  end
244
235
  end
245
236
 
246
- # Takes the environment such as `:production` or `:development`.
237
+ # Takes the environment such as +:production+ or +:development+.
247
238
  # This requires that the @configurations was initialized with a key that
248
239
  # matches.
249
240
  #
@@ -264,7 +255,7 @@ module ActiveRecord
264
255
  # Connection details inside of the "url" key win any merge conflicts
265
256
  def resolve_hash_connection(spec)
266
257
  if spec["url"] && spec["url"] !~ /^jdbc:/
267
- connection_hash = resolve_string_connection(spec.delete("url"))
258
+ connection_hash = resolve_url_connection(spec.delete("url"))
268
259
  spec.merge!(connection_hash)
269
260
  end
270
261
  spec
@@ -1,6 +1,6 @@
1
1
  require 'active_record/connection_adapters/abstract_mysql_adapter'
2
2
 
3
- gem 'mysql2', '~> 0.3.13'
3
+ gem 'mysql2', '>= 0.3.13', '< 0.6.0'
4
4
  require 'mysql2'
5
5
 
6
6
  module ActiveRecord
@@ -20,33 +20,26 @@ module ActiveRecord
20
20
  ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
21
21
  rescue Mysql2::Error => error
22
22
  if error.message.include?("Unknown database")
23
- raise ActiveRecord::NoDatabaseError.new(error.message)
23
+ raise ActiveRecord::NoDatabaseError.new(error.message, error)
24
24
  else
25
- raise error
25
+ raise
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
30
  module ConnectionAdapters
31
31
  class Mysql2Adapter < AbstractMysqlAdapter
32
-
33
- class Column < AbstractMysqlAdapter::Column # :nodoc:
34
- def adapter
35
- Mysql2Adapter
36
- end
37
- end
38
-
39
- ADAPTER_NAME = 'Mysql2'
32
+ ADAPTER_NAME = 'Mysql2'.freeze
40
33
 
41
34
  def initialize(connection, logger, connection_options, config)
42
35
  super
43
- @visitor = BindSubstitution.new self
36
+ @prepared_statements = false
44
37
  configure_connection
45
38
  end
46
39
 
47
40
  MAX_INDEX_LENGTH_FOR_UTF8MB4 = 191
48
41
  def initialize_schema_migrations_table
49
- if @config[:encoding] == 'utf8mb4'
42
+ if charset == 'utf8mb4'
50
43
  ActiveRecord::SchemaMigration.create_table(MAX_INDEX_LENGTH_FOR_UTF8MB4)
51
44
  else
52
45
  ActiveRecord::SchemaMigration.create_table
@@ -69,24 +62,23 @@ module ActiveRecord
69
62
  end
70
63
  end
71
64
 
72
- def new_column(field, default, type, null, collation, extra = "") # :nodoc:
73
- Column.new(field, default, type, null, collation, strict_mode?, extra)
74
- end
75
-
76
65
  def error_number(exception)
77
66
  exception.error_number if exception.respond_to?(:error_number)
78
67
  end
79
68
 
69
+ #--
80
70
  # QUOTING ==================================================
71
+ #++
81
72
 
82
73
  def quote_string(string)
83
74
  @connection.escape(string)
84
75
  end
85
76
 
77
+ #--
86
78
  # CONNECTION MANAGEMENT ====================================
79
+ #++
87
80
 
88
81
  def active?
89
- return false unless @connection
90
82
  @connection.ping
91
83
  end
92
84
 
@@ -101,13 +93,12 @@ module ActiveRecord
101
93
  # Otherwise, this method does nothing.
102
94
  def disconnect!
103
95
  super
104
- unless @connection.nil?
105
- @connection.close
106
- @connection = nil
107
- end
96
+ @connection.close
108
97
  end
109
98
 
99
+ #--
110
100
  # DATABASE STATEMENTS ======================================
101
+ #++
111
102
 
112
103
  def explain(arel, binds = [])
113
104
  sql = "EXPLAIN #{to_sql(arel, binds.dup)}"
@@ -219,11 +210,9 @@ module ActiveRecord
219
210
 
220
211
  # Executes the SQL statement in the context of this connection.
221
212
  def execute(sql, name = nil)
222
- if @connection
223
- # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
224
- # made since we established the connection
225
- @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
226
- end
213
+ # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
214
+ # made since we established the connection
215
+ @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
227
216
 
228
217
  super
229
218
  end
@@ -235,11 +224,6 @@ module ActiveRecord
235
224
 
236
225
  alias exec_without_stmt exec_query
237
226
 
238
- # Returns an ActiveRecord::Result instance.
239
- def select(sql, name = nil, binds = [])
240
- exec_query(sql, name)
241
- end
242
-
243
227
  def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
244
228
  super
245
229
  id_value || @connection.last_id
@@ -273,7 +257,7 @@ module ActiveRecord
273
257
  end
274
258
 
275
259
  def full_version
276
- @full_version ||= @connection.info[:version]
260
+ @full_version ||= @connection.server_info[:version]
277
261
  end
278
262
 
279
263
  def set_field_encoding field_name