Sha256: 28579e32c3e1a23e2b1abcb19b72be101ede85e3585f3e1cbf9fca620876eebd

Contents?: true

Size: 1.11 KB

Versions: 41

Compression:

Stored size: 1.11 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 => 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)
        @watch.detach if @watch && @watch.watching?

        super(*args)
      end

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

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
mysql2-0.4.10-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.10-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.10-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.10 lib/mysql2/em.rb
mysql2-0.4.9-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.9-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.9-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.9 lib/mysql2/em.rb
mysql2-0.4.8-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.8-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.8-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.8 lib/mysql2/em.rb
mysql2-0.4.7-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.7-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.7-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.7 lib/mysql2/em.rb
mysql2-0.4.6-x86-mswin32-60 lib/mysql2/em.rb
mysql2-0.4.6-x86-mingw32 lib/mysql2/em.rb
mysql2-0.4.6-x64-mingw32 lib/mysql2/em.rb
mysql2-0.4.6 lib/mysql2/em.rb