mysql2 0.4.2 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mysql2/em.rb CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'eventmachine'
4
2
  require 'mysql2'
5
3
 
@@ -17,7 +15,7 @@ module Mysql2
17
15
  detach
18
16
  begin
19
17
  result = @client.async_result
20
- rescue => e
18
+ rescue StandardError => e
21
19
  @deferable.fail(e)
22
20
  else
23
21
  @deferable.succeed(result)
@@ -41,7 +39,7 @@ module Mysql2
41
39
 
42
40
  def query(sql, opts = {})
43
41
  if ::EM.reactor_running?
44
- super(sql, opts.merge(:async => true))
42
+ super(sql, opts.merge(async: true))
45
43
  deferable = ::EM::DefaultDeferrable.new
46
44
  @watch = ::EM.watch(socket, Watcher, self, deferable)
47
45
  @watch.notify_readable = true
data/lib/mysql2/error.rb CHANGED
@@ -1,32 +1,65 @@
1
- # encoding: UTF-8
2
-
3
1
  module Mysql2
4
2
  class Error < StandardError
5
3
  ENCODE_OPTS = {
6
- :undef => :replace,
7
- :invalid => :replace,
8
- :replace => '?'.freeze,
4
+ undef: :replace,
5
+ invalid: :replace,
6
+ replace: '?'.freeze,
7
+ }.freeze
8
+
9
+ ConnectionError = Class.new(Error)
10
+ TimeoutError = Class.new(Error)
11
+
12
+ CODES = {
13
+ 1205 => TimeoutError, # ER_LOCK_WAIT_TIMEOUT
14
+
15
+ 1044 => ConnectionError, # ER_DBACCESS_DENIED_ERROR
16
+ 1045 => ConnectionError, # ER_ACCESS_DENIED_ERROR
17
+ 1152 => ConnectionError, # ER_ABORTING_CONNECTION
18
+ 1153 => ConnectionError, # ER_NET_PACKET_TOO_LARGE
19
+ 1154 => ConnectionError, # ER_NET_READ_ERROR_FROM_PIPE
20
+ 1155 => ConnectionError, # ER_NET_FCNTL_ERROR
21
+ 1156 => ConnectionError, # ER_NET_PACKETS_OUT_OF_ORDER
22
+ 1157 => ConnectionError, # ER_NET_UNCOMPRESS_ERROR
23
+ 1158 => ConnectionError, # ER_NET_READ_ERROR
24
+ 1159 => ConnectionError, # ER_NET_READ_INTERRUPTED
25
+ 1160 => ConnectionError, # ER_NET_ERROR_ON_WRITE
26
+ 1161 => ConnectionError, # ER_NET_WRITE_INTERRUPTED
27
+
28
+ 2001 => ConnectionError, # CR_SOCKET_CREATE_ERROR
29
+ 2002 => ConnectionError, # CR_CONNECTION_ERROR
30
+ 2003 => ConnectionError, # CR_CONN_HOST_ERROR
31
+ 2004 => ConnectionError, # CR_IPSOCK_ERROR
32
+ 2005 => ConnectionError, # CR_UNKNOWN_HOST
33
+ 2006 => ConnectionError, # CR_SERVER_GONE_ERROR
34
+ 2007 => ConnectionError, # CR_VERSION_ERROR
35
+ 2009 => ConnectionError, # CR_WRONG_HOST_INFO
36
+ 2012 => ConnectionError, # CR_SERVER_HANDSHAKE_ERR
37
+ 2013 => ConnectionError, # CR_SERVER_LOST
38
+ 2020 => ConnectionError, # CR_NET_PACKET_TOO_LARGE
39
+ 2026 => ConnectionError, # CR_SSL_CONNECTION_ERROR
40
+ 2027 => ConnectionError, # CR_MALFORMED_PACKET
41
+ 2047 => ConnectionError, # CR_CONN_UNKNOW_PROTOCOL
42
+ 2048 => ConnectionError, # CR_INVALID_CONN_HANDLE
43
+ 2049 => ConnectionError, # CR_UNUSED_1
9
44
  }.freeze
10
45
 
11
46
  attr_reader :error_number, :sql_state
12
47
 
13
48
  # Mysql gem compatibility
14
- alias_method :errno, :error_number
15
- alias_method :error, :message
49
+ alias errno error_number
50
+ alias error message
16
51
 
17
- def initialize(msg)
18
- @server_version ||= nil
52
+ def initialize(msg, server_version = nil, error_number = nil, sql_state = nil)
53
+ @server_version = server_version
54
+ @error_number = error_number
55
+ @sql_state = sql_state ? sql_state.encode(ENCODE_OPTS) : nil
19
56
 
20
57
  super(clean_message(msg))
21
58
  end
22
59
 
23
60
  def self.new_with_args(msg, server_version, error_number, sql_state)
24
- err = allocate
25
- err.instance_variable_set('@server_version', server_version)
26
- err.instance_variable_set('@error_number', error_number)
27
- err.instance_variable_set('@sql_state', sql_state.respond_to?(:encode) ? sql_state.encode(ENCODE_OPTS) : sql_state)
28
- err.send(:initialize, msg)
29
- err
61
+ error_class = CODES.fetch(error_number, self)
62
+ error_class.new(msg, server_version, error_number, sql_state)
30
63
  end
31
64
 
32
65
  private
@@ -55,12 +88,8 @@ module Mysql2
55
88
  # encoding, we'll assume UTF-8 and clean the string of anything that's not a
56
89
  # valid UTF-8 character.
57
90
  #
58
- # Except for if we're on 1.8, where we'll do nothing ;)
59
- #
60
- # Returns a valid UTF-8 string in Ruby 1.9+, the original string on Ruby 1.8
91
+ # Returns a valid UTF-8 string.
61
92
  def clean_message(message)
62
- return message unless message.respond_to?(:encode)
63
-
64
93
  if @server_version && @server_version > 50500
65
94
  message.encode(ENCODE_OPTS)
66
95
  else
data/lib/mysql2/result.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module Mysql2
2
2
  class Result
3
+ attr_reader :server_flags
4
+
3
5
  include Enumerable
4
6
  end
5
7
  end
@@ -2,15 +2,9 @@ module Mysql2
2
2
  class Statement
3
3
  include Enumerable
4
4
 
5
- if Thread.respond_to?(:handle_interrupt)
6
- def execute(*args)
7
- Thread.handle_interrupt(::Mysql2::Util::TimeoutError => :never) do
8
- _execute(*args)
9
- end
10
- end
11
- else
12
- def execute(*args)
13
- _execute(*args)
5
+ def execute(*args, **kwargs)
6
+ Thread.handle_interrupt(::Mysql2::Util::TIMEOUT_ERROR_CLASS => :never) do
7
+ _execute(*args, **kwargs)
14
8
  end
15
9
  end
16
10
  end
@@ -1,3 +1,3 @@
1
1
  module Mysql2
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.2".freeze
3
3
  end
data/lib/mysql2.rb CHANGED
@@ -1,7 +1,5 @@
1
- # encoding: UTF-8
2
1
  require 'date'
3
2
  require 'bigdecimal'
4
- require 'rational' unless RUBY_VERSION >= '1.9.2'
5
3
 
6
4
  # Load libmysql.dll before requiring mysql2/mysql2.so
7
5
  # This gives a chance to be flexible about the load path
@@ -13,16 +11,20 @@ if RUBY_PLATFORM =~ /mswin|mingw/
13
11
  ENV['RUBY_MYSQL2_LIBMYSQL_DLL']
14
12
  elsif File.exist?(File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)))
15
13
  # Use vendor/libmysql.dll if it exists, convert slashes for Win32 LoadLibrary
16
- File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)).tr('/', '\\')
14
+ File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__))
15
+ elsif defined?(RubyInstaller)
16
+ # RubyInstaller-2.4+ native build doesn't need DLL preloading
17
17
  else
18
18
  # This will use default / system library paths
19
19
  'libmysql.dll'
20
20
  end
21
21
 
22
- require 'Win32API'
23
- LoadLibrary = Win32API.new('Kernel32', 'LoadLibrary', ['P'], 'I')
24
- if 0 == LoadLibrary.call(dll_path)
25
- abort "Failed to load libmysql.dll from #{dll_path}"
22
+ if dll_path
23
+ require 'Win32API'
24
+ LoadLibrary = Win32API.new('Kernel32', 'LoadLibrary', ['P'], 'I')
25
+ if LoadLibrary.call(dll_path).zero?
26
+ abort "Failed to load libmysql.dll from #{dll_path}"
27
+ end
26
28
  end
27
29
  end
28
30
 
@@ -71,14 +73,11 @@ module Mysql2
71
73
  # Timeout::ExitException was removed in Ruby 2.3.0, 2.2.3, and 2.1.8,
72
74
  # but is present in earlier 2.1.x and 2.2.x, so we provide a shim.
73
75
  #
74
- if Thread.respond_to?(:handle_interrupt)
75
- require 'timeout'
76
- # rubocop:disable Style/ConstantName
77
- TimeoutError = if defined?(::Timeout::ExitException)
78
- ::Timeout::ExitException
79
- else
80
- ::Timeout::Error
81
- end
76
+ require 'timeout'
77
+ TIMEOUT_ERROR_CLASS = if defined?(::Timeout::ExitException)
78
+ ::Timeout::ExitException
79
+ else
80
+ ::Timeout::Error
82
81
  end
83
82
  end
84
83
  end
@@ -9,9 +9,3 @@ user:
9
9
  username: LOCALUSERNAME
10
10
  password:
11
11
  database: mysql2_test
12
-
13
- numericuser:
14
- host: localhost
15
- username: LOCALUSERNAME
16
- password:
17
- database: 12345
data/spec/em/em_spec.rb CHANGED
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
  begin
4
3
  require 'eventmachine'
@@ -49,13 +48,13 @@ begin
49
48
  end
50
49
 
51
50
  it "should not swallow exceptions raised in callbacks" do
52
- expect {
51
+ expect do
53
52
  EM.run do
54
53
  client = Mysql2::EM::Client.new DatabaseCredentials['root']
55
54
  defer = client.query "SELECT sleep(0.1) as first_query"
56
55
  defer.callback do
57
56
  client.close
58
- fail 'some error'
57
+ raise 'some error'
59
58
  end
60
59
  defer.errback do
61
60
  # This _shouldn't_ be run, but it needed to prevent the specs from
@@ -63,13 +62,14 @@ begin
63
62
  EM.stop_event_loop
64
63
  end
65
64
  end
66
- }.to raise_error('some error')
65
+ end.to raise_error('some error')
67
66
  end
68
67
 
69
68
  context 'when an exception is raised by the client' do
70
69
  let(:client) { Mysql2::EM::Client.new DatabaseCredentials['root'] }
71
70
  let(:error) { StandardError.new('some error') }
72
71
  before { allow(client).to receive(:async_result).and_raise(error) }
72
+ after { client.close }
73
73
 
74
74
  it "should swallow exceptions raised in by the client" do
75
75
  errors = []
@@ -122,9 +122,9 @@ begin
122
122
  end
123
123
  EM.add_timer(0.1) do
124
124
  expect(callbacks_run).to eq([:callback])
125
- expect {
125
+ expect do
126
126
  client.close
127
- }.not_to raise_error
127
+ end.not_to raise_error
128
128
  EM.stop_event_loop
129
129
  end
130
130
  end