Sha256: 74d02ed66b129791dba73d7dbc40256b206194fcad51c2e122b3e1ec2d166e25

Contents?: true

Size: 1.14 KB

Versions: 24

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8

require 'eventmachine'
require 'mysql2'

module Mysql2
  module EM
    class Client < ::Mysql2::Client
      module Watcher
        def initialize(client, deferable)
          @client = client
          @deferable = deferable
          @is_watching = true
        end

        def notify_readable
          detach
          begin
            result = @client.async_result
          rescue Exception => e
            @deferable.fail(e)
          else
            @deferable.succeed(result)
          end
        end

        def watching?
          @is_watching
        end

        def unbind
          @is_watching = false
        end
      end

      def close(*args)
        if @watch
          @watch.detach if @watch.watching?
        end
        super(*args)
      end

      def query(sql, opts={})
        if ::EM.reactor_running?
          super(sql, opts.merge(:async => true))
          deferable = ::EM::DefaultDeferrable.new
          @watch = ::EM.watch(self.socket, Watcher, self, deferable)
          @watch.notify_readable = true
          deferable
        else
          super(sql, opts)
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
mysql2-0.3.21-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.3.21-x86-mingw32 lib/mysql2/em.rb
mysql2-0.3.21-x64-mingw32 lib/mysql2/em.rb
mysql2-0.3.21 lib/mysql2/em.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/mysql2-0.3.20/lib/mysql2/em.rb
mysql2-0.4.0-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.0-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.0-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.0 lib/mysql2/em.rb
mysql2-0.3.20-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.3.20-x86-mingw32 lib/mysql2/em.rb
mysql2-0.3.20-x64-mingw32 lib/mysql2/em.rb
mysql2-0.3.20 lib/mysql2/em.rb
mysql2-0.3.19-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.3.19-x86-mingw32 lib/mysql2/em.rb
mysql2-0.3.19-x64-mingw32 lib/mysql2/em.rb
mysql2-0.3.19 lib/mysql2/em.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/mysql2-0.3.18/lib/mysql2/em.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/mysql2-0.3.18/lib/mysql2/em.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/mysql2-0.3.18/lib/mysql2/em.rb