Class: Mysql::Result

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

Overview

Result set for simple query

Instance Attribute Summary

Attributes inherited from ResultBase

#fields, #result

Instance Method Summary collapse

Methods inherited from ResultBase

#data_seek, #each, #each_hash, #fetch_hash, #free, #retrieve, #row_seek, #row_tell, #server_status, #size

Constructor Details

#initialize(fields, protocol = nil, **opts) ⇒ Result

Returns a new instance of Result.

Parameters:



137
138
139
140
141
142
# File 'lib/mysql/result.rb', line 137

def initialize(fields, protocol=nil, **opts)
  super fields, protocol, RawRecord, **opts
  return unless protocol
  fields.each{|f| f.result = self}  # for calculating max_field
  retrieve if @opts.merge(opts)[:auto_store_result]
end

Instance Method Details

#calculate_field_max_lengthObject

calculate max_length of all fields



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mysql/result.rb', line 153

def calculate_field_max_length
  return unless @records
  max_length = Array.new(@fields.size, 0)
  @records.each_with_index do |rec, i|
    rec = @records[i] = rec.to_a if rec.is_a? RawRecord
    max_length.each_index do |j|
      max_length[j] = rec[j].to_s.length if rec[j] && rec[j].to_s.length > max_length[j]
    end
  end
  max_length.each_with_index do |len, i|
    @fields[i].max_length = len
  end
end

#fetch(**opts) ⇒ Object Also known as: fetch_row



144
145
146
147
148
# File 'lib/mysql/result.rb', line 144

def fetch(**opts)
  rec = super
  rec = rec.map.with_index{|s, i| convert_type(@fields[i], s)} if rec && @opts.merge(opts)[:cast]
  rec
end