Class: EmMysql

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

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ EmMysql

Returns a new instance of EmMysql.



256
257
258
259
# File 'lib/em/mysql.rb', line 256

def initialize(settings = {})
  @queue = []
  @settings = { :connections => 4, :logging => false, :em_mysql => self }.merge settings
end

Instance Method Details

#all(query, type = nil, &blk) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/em/mysql.rb', line 288

def all query, type = nil, &blk
  responses = 0
  connection_pool.each do |c|
    c.execute(query, type) do
      responses += 1
      blk.call if blk and responses == @connection_pool.size
    end
  end
end

#connection_poolObject



298
299
300
301
302
303
304
305
# File 'lib/em/mysql.rb', line 298

def connection_pool
  @connection_pool ||= (1..settings[:connections]).map{ EventedMysql.connect(settings) }
  # p ['connpool', settings[:connections], @connection_pool.size]
  # (1..(settings[:connections]-@connection_pool.size)).each do
  #   @connection_pool << EventedMysql.connect(settings)
  # end unless settings[:connections] == @connection_pool.size
  # @connection_pool
end

#execute(query, type = nil, cblk = nil, eblk = nil, &blk) ⇒ Object



269
270
271
272
273
274
275
276
277
278
# File 'lib/em/mysql.rb', line 269

def execute query, type = nil, cblk = nil, eblk = nil, &blk
  unless nil#connection = connection_pool.find{|c| not c.processing and c.connected }
    @n ||= 0

    connection = connection_pool[@n]
    @n = 0 if (@n+=1) >= connection_pool.size
  end
  
  connection.execute(query, type, cblk, eblk, &blk)
end

#queueObject



261
262
263
# File 'lib/em/mysql.rb', line 261

def queue
  @queue
end

#settingsObject



265
266
267
# File 'lib/em/mysql.rb', line 265

def settings
  @settings 
end