Class: DB::MariaDB::Native::Result

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/db/mariadb/native/result.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, types = {}, address) ⇒ Result

Returns a new instance of Result.



35
36
37
38
39
40
41
42
# File 'lib/db/mariadb/native/result.rb', line 35

def initialize(connection, types = {}, address)
  super(address)
  
  @connection = connection
  @fields = nil
  @types = types
  @casts = nil
end

Instance Method Details

#cast!(row) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/db/mariadb/native/result.rb', line 75

def cast!(row)
  @casts ||= self.field_types
  
  row.size.times do |index|
    if cast = @casts[index]
      row[index] = cast.parse(row[index])
    end
  end
  
  return row
end

#eachObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/db/mariadb/native/result.rb', line 87

def each
  row = FFI::MemoryPointer.new(:pointer)
  field_count = self.field_count
  
  while true
    status = Native.mysql_fetch_row_start(row, self)
    
    while status != 0
      @connection.wait_for(status)
      
      status = Native.mysql_fetch_row_cont(row, self, status)
    end
    
    pointer = row.read_pointer
    
    if pointer.null?
      break
    else
      yield cast!(pointer.get_array_of_string(0, field_count))
    end
  end
  
  @connection.check_error!("Reading recordset")
end

#field_countObject



44
45
46
# File 'lib/db/mariadb/native/result.rb', line 44

def field_count
  Native.mysql_num_fields(self)
end

#field_namesObject Also known as: keys



60
61
62
# File 'lib/db/mariadb/native/result.rb', line 60

def field_names
  fields.map(&:name)
end

#field_typesObject



64
65
66
# File 'lib/db/mariadb/native/result.rb', line 64

def field_types
  fields.map{|field| @types[field.type]}
end

#fieldsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/db/mariadb/native/result.rb', line 48

def fields
  unless @fields
    pointer = Native.mysql_fetch_fields(self)
    
    @fields = field_count.times.map do |index|
      Field.new(pointer +  index * Field.size)
    end
  end
  
  return @fields
end

#map(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/db/mariadb/native/result.rb', line 112

def map(&block)
  results = []
  
  self.each do |row|
    results << yield(row)
  end
  
  return results
end

#row_countObject Also known as: count



68
69
70
# File 'lib/db/mariadb/native/result.rb', line 68

def row_count
  Native.mysql_num_rows(self)
end

#to_aObject



122
123
124
125
126
127
128
129
130
# File 'lib/db/mariadb/native/result.rb', line 122

def to_a
  rows = []
  
  self.each do |row|
    rows << row
  end
  
  return rows
end