Class: Baza::Driver::Mysql::Result

Inherits:
ResultBase show all
Defined in:
lib/baza/driver/mysql/result.rb

Overview

This class controls the results for the normal MySQL-driver.

Constant Summary collapse

INT_TYPES =
{
  ::Mysql::Field::TYPE_DECIMAL => true,
  ::Mysql::Field::TYPE_TINY => true,
  ::Mysql::Field::TYPE_LONG => true,
  ::Mysql::Field::TYPE_YEAR => true
}.freeze
FLOAT_TYPES =
{
  ::Mysql::Field::TYPE_DECIMAL => true,
  ::Mysql::Field::TYPE_FLOAT => true,
  ::Mysql::Field::TYPE_DOUBLE => true
}.freeze
TIME_TYPES =
{
  ::Mysql::Field::TYPE_DATETIME => true
}.freeze
DATE_TYPES =
{
  ::Mysql::Field::TYPE_DATE => true
}.freeze

Instance Method Summary collapse

Methods inherited from ResultBase

#to_a, #to_a_enum, #to_enum

Constructor Details

#initialize(driver, result) ⇒ Result

Constructor. This should not be called manually.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/baza/driver/mysql/result.rb', line 22

def initialize(driver, result)
  @driver = driver
  @result = result
  @mutex = Mutex.new
  @type_translation = driver.db.opts[:type_translation]

  return unless @result

  @keys = []
  @types = [] if @type_translation

  @result.fetch_fields.each do |key|
    @keys << key.name.to_sym
    @types << key.type if @type_translation
  end
end

Instance Method Details

#eachObject

Loops over every result yielding it.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/baza/driver/mysql/result.rb', line 60

def each
  loop do
    data = fetch

    if data
      yield data
    else
      break
    end
  end
end

#fetchObject

Returns a single result as a hash with symbols as keys.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/baza/driver/mysql/result.rb', line 40

def fetch
  fetched = nil
  @mutex.synchronize do
    fetched = @result.fetch_row
  end

  return false unless fetched

  if @type_translation == true
    fetched.collect!.with_index do |value, count|
      translate_value_to_type(value, @types[count])
    end
  elsif @type_translation == :string
    fetched.collect!(&:to_s)
  end

  Hash[*@keys.zip(fetched).flatten]
end