Class: Baza::Driver::Mysql::UnbufferedResult

Inherits:
ResultBase
  • Object
show all
Defined in:
lib/baza/driver/mysql/unbuffered_result.rb

Overview

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

Instance Method Summary collapse

Methods inherited from ResultBase

#to_a, #to_a_enum, #to_enum

Constructor Details

#initialize(conn, opts, result) ⇒ UnbufferedResult

Constructor. This should not be called manually.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/baza/driver/mysql/unbuffered_result.rb', line 4

def initialize(conn, opts, result)
  @conn = conn
  @result = result

  if !opts.key?(:result) || 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.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/baza/driver/mysql/unbuffered_result.rb', line 52

def each
  loop do
    row = fetch

    if row
      yield row
    else
      break
    end
  end
end

#fetchObject

Returns a single result.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/baza/driver/mysql/unbuffered_result.rb', line 18

def fetch
  if @enum
    begin
      ret = @enum.next
    rescue StopIteration
      @enum = nil
      @res = nil
    end
  end

  if !ret && !@res && !@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
    load_keys unless @keys
    return Hash[*@keys.zip(ret).flatten]
  else
    return ret
  end
end