Class: DB::MySQL::Native::Result

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

Instance Method Summary collapse

Constructor Details

#initialize(connection, address) ⇒ Result

Returns a new instance of Result.



98
99
100
101
102
103
# File 'lib/db/mysql/native/result.rb', line 98

def initialize(connection, address)
  super(address)
  
  @connection = connection
  @fields = nil
end

Instance Method Details

#eachObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/db/mysql/native/result.rb', line 132

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 pointer.get_array_of_string(0, field_count)
    end
  end
  
  @connection.check_error!("Reading recordset")
end

#field_countObject



105
106
107
# File 'lib/db/mysql/native/result.rb', line 105

def field_count
  Native.mysql_num_fields(self)
end

#field_namesObject Also known as: keys



121
122
123
# File 'lib/db/mysql/native/result.rb', line 121

def field_names
  fields.map(&:name)
end

#fieldsObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/db/mysql/native/result.rb', line 109

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

#row_countObject Also known as: count



125
126
127
# File 'lib/db/mysql/native/result.rb', line 125

def row_count
  Native.mysql_num_rows(self)
end

#to_aObject



157
158
159
160
161
162
163
164
165
# File 'lib/db/mysql/native/result.rb', line 157

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