activerecord 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +776 -1330
  3. data/README.rdoc +15 -10
  4. data/lib/active_record/aggregations.rb +12 -8
  5. data/lib/active_record/association_relation.rb +4 -0
  6. data/lib/active_record/associations/alias_tracker.rb +14 -13
  7. data/lib/active_record/associations/association.rb +2 -2
  8. data/lib/active_record/associations/association_scope.rb +83 -43
  9. data/lib/active_record/associations/belongs_to_association.rb +15 -5
  10. data/lib/active_record/associations/builder/association.rb +15 -4
  11. data/lib/active_record/associations/builder/belongs_to.rb +7 -29
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +9 -6
  13. data/lib/active_record/associations/builder/has_many.rb +1 -1
  14. data/lib/active_record/associations/builder/has_one.rb +2 -2
  15. data/lib/active_record/associations/builder/singular_association.rb +8 -1
  16. data/lib/active_record/associations/collection_association.rb +66 -29
  17. data/lib/active_record/associations/collection_proxy.rb +22 -26
  18. data/lib/active_record/associations/has_many_association.rb +65 -18
  19. data/lib/active_record/associations/has_many_through_association.rb +55 -27
  20. data/lib/active_record/associations/has_one_association.rb +0 -1
  21. data/lib/active_record/associations/join_dependency/join_association.rb +19 -15
  22. data/lib/active_record/associations/join_dependency/join_part.rb +0 -1
  23. data/lib/active_record/associations/join_dependency.rb +20 -12
  24. data/lib/active_record/associations/preloader/association.rb +34 -11
  25. data/lib/active_record/associations/preloader/through_association.rb +4 -3
  26. data/lib/active_record/associations/preloader.rb +49 -59
  27. data/lib/active_record/associations/singular_association.rb +25 -4
  28. data/lib/active_record/associations/through_association.rb +23 -14
  29. data/lib/active_record/associations.rb +171 -42
  30. data/lib/active_record/attribute.rb +149 -0
  31. data/lib/active_record/attribute_assignment.rb +18 -10
  32. data/lib/active_record/attribute_decorators.rb +66 -0
  33. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -2
  34. data/lib/active_record/attribute_methods/dirty.rb +98 -44
  35. data/lib/active_record/attribute_methods/primary_key.rb +14 -8
  36. data/lib/active_record/attribute_methods/query.rb +1 -1
  37. data/lib/active_record/attribute_methods/read.rb +22 -59
  38. data/lib/active_record/attribute_methods/serialization.rb +37 -147
  39. data/lib/active_record/attribute_methods/time_zone_conversion.rb +34 -28
  40. data/lib/active_record/attribute_methods/write.rb +14 -21
  41. data/lib/active_record/attribute_methods.rb +67 -94
  42. data/lib/active_record/attribute_set/builder.rb +86 -0
  43. data/lib/active_record/attribute_set.rb +77 -0
  44. data/lib/active_record/attributes.rb +139 -0
  45. data/lib/active_record/autosave_association.rb +45 -38
  46. data/lib/active_record/base.rb +10 -20
  47. data/lib/active_record/callbacks.rb +7 -7
  48. data/lib/active_record/coders/json.rb +13 -0
  49. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +78 -52
  50. data/lib/active_record/connection_adapters/abstract/database_statements.rb +38 -59
  51. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -0
  52. data/lib/active_record/connection_adapters/abstract/quoting.rb +59 -55
  53. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +46 -5
  54. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +126 -54
  55. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -34
  56. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +198 -64
  57. data/lib/active_record/connection_adapters/abstract/transaction.rb +126 -114
  58. data/lib/active_record/connection_adapters/abstract_adapter.rb +154 -55
  59. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +240 -135
  60. data/lib/active_record/connection_adapters/column.rb +28 -239
  61. data/lib/active_record/connection_adapters/connection_specification.rb +16 -25
  62. data/lib/active_record/connection_adapters/mysql2_adapter.rb +20 -22
  63. data/lib/active_record/connection_adapters/mysql_adapter.rb +65 -149
  64. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +15 -27
  65. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  66. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +39 -27
  67. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +99 -0
  68. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  69. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  70. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +14 -0
  71. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  73. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +27 -0
  74. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +17 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  78. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  79. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  80. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  81. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  82. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  83. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +97 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid.rb +29 -374
  93. data/lib/active_record/connection_adapters/postgresql/quoting.rb +55 -135
  94. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +4 -4
  95. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  96. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +127 -38
  97. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  98. data/lib/active_record/connection_adapters/postgresql_adapter.rb +220 -466
  99. data/lib/active_record/connection_adapters/schema_cache.rb +14 -28
  100. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +66 -61
  101. data/lib/active_record/connection_handling.rb +3 -3
  102. data/lib/active_record/core.rb +143 -32
  103. data/lib/active_record/counter_cache.rb +60 -7
  104. data/lib/active_record/enum.rb +10 -11
  105. data/lib/active_record/errors.rb +49 -27
  106. data/lib/active_record/explain.rb +1 -1
  107. data/lib/active_record/fixtures.rb +56 -70
  108. data/lib/active_record/gem_version.rb +2 -2
  109. data/lib/active_record/inheritance.rb +35 -10
  110. data/lib/active_record/integration.rb +4 -4
  111. data/lib/active_record/locking/optimistic.rb +35 -17
  112. data/lib/active_record/log_subscriber.rb +1 -1
  113. data/lib/active_record/migration/command_recorder.rb +19 -2
  114. data/lib/active_record/migration/join_table.rb +1 -1
  115. data/lib/active_record/migration.rb +52 -49
  116. data/lib/active_record/model_schema.rb +49 -57
  117. data/lib/active_record/nested_attributes.rb +7 -7
  118. data/lib/active_record/null_relation.rb +19 -5
  119. data/lib/active_record/persistence.rb +50 -31
  120. data/lib/active_record/query_cache.rb +3 -3
  121. data/lib/active_record/querying.rb +10 -7
  122. data/lib/active_record/railtie.rb +14 -11
  123. data/lib/active_record/railties/databases.rake +56 -54
  124. data/lib/active_record/readonly_attributes.rb +0 -1
  125. data/lib/active_record/reflection.rb +286 -102
  126. data/lib/active_record/relation/batches.rb +0 -1
  127. data/lib/active_record/relation/calculations.rb +39 -31
  128. data/lib/active_record/relation/delegation.rb +2 -2
  129. data/lib/active_record/relation/finder_methods.rb +80 -36
  130. data/lib/active_record/relation/merger.rb +25 -30
  131. data/lib/active_record/relation/predicate_builder/array_handler.rb +31 -13
  132. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -1
  133. data/lib/active_record/relation/predicate_builder.rb +11 -10
  134. data/lib/active_record/relation/query_methods.rb +141 -55
  135. data/lib/active_record/relation/spawn_methods.rb +3 -0
  136. data/lib/active_record/relation.rb +69 -30
  137. data/lib/active_record/result.rb +18 -7
  138. data/lib/active_record/sanitization.rb +12 -2
  139. data/lib/active_record/schema.rb +0 -1
  140. data/lib/active_record/schema_dumper.rb +58 -26
  141. data/lib/active_record/schema_migration.rb +11 -0
  142. data/lib/active_record/scoping/default.rb +8 -7
  143. data/lib/active_record/scoping/named.rb +4 -0
  144. data/lib/active_record/serializers/xml_serializer.rb +3 -7
  145. data/lib/active_record/statement_cache.rb +95 -10
  146. data/lib/active_record/store.rb +19 -10
  147. data/lib/active_record/tasks/database_tasks.rb +73 -7
  148. data/lib/active_record/tasks/mysql_database_tasks.rb +3 -2
  149. data/lib/active_record/tasks/postgresql_database_tasks.rb +1 -1
  150. data/lib/active_record/tasks/sqlite_database_tasks.rb +5 -1
  151. data/lib/active_record/timestamp.rb +11 -9
  152. data/lib/active_record/transactions.rb +37 -21
  153. data/lib/active_record/type/big_integer.rb +13 -0
  154. data/lib/active_record/type/binary.rb +50 -0
  155. data/lib/active_record/type/boolean.rb +30 -0
  156. data/lib/active_record/type/date.rb +46 -0
  157. data/lib/active_record/type/date_time.rb +43 -0
  158. data/lib/active_record/type/decimal.rb +40 -0
  159. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  160. data/lib/active_record/type/decorator.rb +14 -0
  161. data/lib/active_record/type/float.rb +19 -0
  162. data/lib/active_record/type/hash_lookup_type_map.rb +17 -0
  163. data/lib/active_record/type/integer.rb +55 -0
  164. data/lib/active_record/type/mutable.rb +16 -0
  165. data/lib/active_record/type/numeric.rb +36 -0
  166. data/lib/active_record/type/serialized.rb +56 -0
  167. data/lib/active_record/type/string.rb +36 -0
  168. data/lib/active_record/type/text.rb +11 -0
  169. data/lib/active_record/type/time.rb +26 -0
  170. data/lib/active_record/type/time_value.rb +38 -0
  171. data/lib/active_record/type/type_map.rb +64 -0
  172. data/lib/active_record/type/unsigned_integer.rb +15 -0
  173. data/lib/active_record/type/value.rb +101 -0
  174. data/lib/active_record/type.rb +23 -0
  175. data/lib/active_record/validations/associated.rb +5 -3
  176. data/lib/active_record/validations/presence.rb +6 -4
  177. data/lib/active_record/validations/uniqueness.rb +11 -17
  178. data/lib/active_record/validations.rb +25 -19
  179. data/lib/active_record.rb +3 -0
  180. data/lib/rails/generators/active_record/migration/migration_generator.rb +8 -4
  181. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +4 -1
  182. data/lib/rails/generators/active_record/migration/templates/migration.rb +6 -0
  183. data/lib/rails/generators/active_record/model/templates/model.rb +1 -1
  184. metadata +65 -10
  185. 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
+ :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)
33
+ def initialize(name, default, cast_type, sql_type = nil, null = true)
30
34
  @name = name
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
  #
@@ -86,26 +86,13 @@ module ActiveRecord
86
86
  "password" => uri.password,
87
87
  "port" => uri.port,
88
88
  "database" => database_from_path,
89
- "host" => uri.host })
89
+ "host" => uri.hostname })
90
90
  end
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
@@ -20,27 +20,20 @@ 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
 
@@ -69,21 +62,29 @@ 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
+ def quoted_date(value)
78
+ if value.acts_like?(:time) && value.respond_to?(:usec)
79
+ "#{super}.#{sprintf("%06d", value.usec)}"
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ #--
86
86
  # CONNECTION MANAGEMENT ====================================
87
+ #++
87
88
 
88
89
  def active?
89
90
  return false unless @connection
@@ -107,7 +108,9 @@ module ActiveRecord
107
108
  end
108
109
  end
109
110
 
111
+ #--
110
112
  # DATABASE STATEMENTS ======================================
113
+ #++
111
114
 
112
115
  def explain(arel, binds = [])
113
116
  sql = "EXPLAIN #{to_sql(arel, binds.dup)}"
@@ -235,11 +238,6 @@ module ActiveRecord
235
238
 
236
239
  alias exec_without_stmt exec_query
237
240
 
238
- # Returns an ActiveRecord::Result instance.
239
- def select(sql, name = nil, binds = [])
240
- exec_query(sql, name)
241
- end
242
-
243
241
  def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
244
242
  super
245
243
  id_value || @connection.last_id
@@ -272,8 +270,8 @@ module ActiveRecord
272
270
  super
273
271
  end
274
272
 
275
- def version
276
- @version ||= @connection.info[:version].scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
273
+ def full_version
274
+ @full_version ||= @connection.info[:version]
277
275
  end
278
276
 
279
277
  def set_field_encoding field_name