Sha256: 8dc6a01d2140dd94a18917ca0bf41f16b919ce2084eca89d7e1fd517261b2adc

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'sequel'
require 'sequel/adapters/postgres'

# Management of Postgres Sockets
POSTGRES_SOCKETS = {}

##
# Midori Extension of sequel postgres through meta programming
class Sequel::Postgres::Adapter
  # Call a sql request asynchronously
  # @param [String] sql sql request
  # @param [Array] args args to send
  # @return [Array] sql query result
  alias_method :execute_query_block, :execute_query

  def execute_query(sql, args)
    if Fiber.current == EventLoop.root_fiber
      # Block usage
      return execute_query_block(sql, args)
    else
      # Nonblock usage
      return execute_query_nonblock(sql, args)
    end
  end

  def execute_query_nonblock(sql, args)
    @db.log_connection_yield(sql, self, args) do
      if POSTGRES_SOCKETS[self].nil?
        POSTGRES_SOCKETS[self] = IO::open(socket)
      end
      socket_object = POSTGRES_SOCKETS[self]
      await(Promise.new do |resolve|
        count = 0
        EventLoop.register(socket_object, :rw) do
          begin
            if (count == 0)
              # Writable
              unless is_busy
                send_query(sql)
                count += 1
              end
            else
              # Readable
              EventLoop.deregister(socket_object)
              resolve.call(get_result)
            end
          # For extra errors
          # :nocov:
          rescue => e
            resolve.call(PromiseException.new(e))
          # :nocov:
          end
        end
      end)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
midori-contrib-0.1.3 lib/midori-contrib/sequel/postgres.rb