Class: EmMysql2ConnectionPool::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/em_mysql2_connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(sql, opts, deferrable) ⇒ Query

Returns a new instance of Query.



7
8
9
# File 'lib/em_mysql2_connection_pool.rb', line 7

def initialize(sql, opts, deferrable)
  @sql, @opts, @deferrable = sql, opts, deferrable
end

Instance Method Details

#default_errbackObject



47
48
49
# File 'lib/em_mysql2_connection_pool.rb', line 47

def default_errback
  proc{ |error, sql| puts "#{error.class}: '#{error.message}' with query #{sql} in #{error.backtrace.first}" }
end

#execute(connection, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/em_mysql2_connection_pool.rb', line 15

def execute(connection, &block)
  @busy = true
  @query_text = sql(connection)
  q = connection.query @query_text, @opts
  q.callback{ |result| succeed result, connection.affected_rows, &block }
  q.errback{  |error| fail error, &block }
  return q
rescue StandardError => error
  fail error, &block
end

#fail(error, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/em_mysql2_connection_pool.rb', line 35

def fail(error, &block)
  @deferrable.errback &default_errback unless has_errbacks?
  @deferrable.fail error, @query_text
ensure
  @busy and block and block.call
  @busy = false
end

#has_errbacks?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/em_mysql2_connection_pool.rb', line 43

def has_errbacks?
  !@deferrable.errbacks.nil?
end

#sql(connection) ⇒ Object



11
12
13
# File 'lib/em_mysql2_connection_pool.rb', line 11

def sql(connection)
  @sql.respond_to?(:call) ? @sql.call(connection) : @sql
end

#succeed(result, affected_rows, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/em_mysql2_connection_pool.rb', line 26

def succeed(result, affected_rows, &block)
  @deferrable.succeed result, affected_rows
rescue StandardError => error
  fail error, &block
ensure
  @busy and block and block.call
  @busy = false
end