2
2
3
3
module Mysql2
4
4
class Error < StandardError
5
- REPLACEMENT_CHAR = '?'
6
- ENCODE_OPTS = { :undef => :replace , :invalid => :replace , :replace => REPLACEMENT_CHAR }
5
+ ENCODE_OPTS = {
6
+ :undef => :replace ,
7
+ :invalid => :replace ,
8
+ :replace => '?' . freeze ,
9
+ } . freeze
7
10
8
11
attr_accessor :error_number
9
12
attr_reader :sql_state
@@ -20,7 +23,7 @@ def initialize(msg, server_version=nil)
20
23
end
21
24
22
25
def sql_state = ( state )
23
- @sql_state = '' . respond_to? ( :encode ) ? state . encode ( ENCODE_OPTS ) : state
26
+ @sql_state = state . respond_to? ( :encode ) ? state . encode ( ENCODE_OPTS ) : state
24
27
end
25
28
26
29
private
@@ -53,27 +56,12 @@ def sql_state=(state)
53
56
#
54
57
# Returns a valid UTF-8 string in Ruby 1.9+, the original string on Ruby 1.8
55
58
def clean_message ( message )
56
- return message if ! message . respond_to? ( :encoding )
59
+ return message unless message . respond_to? ( :encode )
57
60
58
61
if @server_version && @server_version > 50500
59
62
message . encode ( ENCODE_OPTS )
60
63
else
61
- if message . respond_to? :scrub
62
- message . scrub ( REPLACEMENT_CHAR ) . encode ( ENCODE_OPTS )
63
- else
64
- # This is ugly as hell but Ruby 1.9 doesn't provide a way to clean a string
65
- # and retain it's valid UTF-8 characters, that I know of.
66
-
67
- new_message = "" . force_encoding ( Encoding ::UTF_8 )
68
- message . chars . each do |char |
69
- if char . valid_encoding?
70
- new_message << char
71
- else
72
- new_message << REPLACEMENT_CHAR
73
- end
74
- end
75
- new_message . encode ( ENCODE_OPTS )
76
- end
64
+ message . encode ( Encoding ::UTF_8 , ENCODE_OPTS )
77
65
end
78
66
end
79
67
end
0 commit comments