Class: KnjDB_mysql_unbuffered_result

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb

Overview

This class controls the unbuffered result for the normal MySQL-driver.

Instance Method Summary collapse

Constructor Details

#initialize(conn, opts, result) ⇒ KnjDB_mysql_unbuffered_result

Constructor. This should not be called manually.



445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 445

def initialize(conn, opts, result)
  @conn = conn
  @result = result
  
  if !opts.key?(:result) or opts[:result] == "hash"
    @as_hash = true
  elsif opts[:result] == "array"
    @as_hash = false
  else
    raise "Unknown type of result: '#{opts[:result]}'."
  end
end

Instance Method Details

#eachObject

Loops over every single result yielding it.



508
509
510
511
512
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 508

def each
  while data = self.fetch
    yield(data)
  end
end

#fetchObject

Returns a single result.



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 468

def fetch
  if @enum
    begin
      ret = @enum.next
    rescue StopIteration
      @enum = nil
      @res = nil
    end
  end
  
  if !ret and !@res and !@enum
    begin
      @res = @conn.use_result
      @enum = @res.to_enum
      ret = @enum.next
    rescue Mysql::Error
      #Reset it to run non-unbuffered again and then return false.
      @conn.query_with_result = true
      return false
    rescue StopIteration
      sleep 0.1
      retry
    end
  end
  
  if !@as_hash
    return ret
  else
    self.load_keys if !@keys
    
    ret_h = {}
    @keys.each_index do |key_no|
      ret_h[@keys[key_no]] = ret[key_no]
    end
    
    return ret_h
  end
end

#load_keysObject

Lods the keys for the object.



459
460
461
462
463
464
465
# File 'lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb', line 459

def load_keys
  @keys = []
  keys = @res.fetch_fields
  keys.each do |key|
    @keys << key.name.to_sym
  end
end