class Mysql::ResultBase
@!visibility public Result set
Attributes
@return [Integer] number of record
@return [Array<Mysql::Field>] field list
@return [Array<Mysql::Field>] field list
@return [Integer] number of record
@return [Mysql::StatementResult]
@return [Integer] number of record
Public Class Methods
Source
# File lib/mysql/result.rb, line 15 def initialize(fields, protocol, record_class, **opts) @fields = fields @field_index = 0 # index of field @records = nil # all records @index = 0 # index of record @size = 0 # retrieved record count @fieldname_with_table = nil @protocol = protocol @record_class = record_class @opts = opts end
@param [Array of Mysql::Field] fields
Public Instance Methods
Source
# File lib/mysql/result.rb, line 106 def data_seek(n) @index = n self end
Set record position @param [Integer] n record index @return [self] self
Source
# File lib/mysql/result.rb, line 81 def each(**opts, &block) @index = 0 return enum_for(:each, **opts) unless block while (rec = fetch(**opts)) block.call rec end self end
Iterate block with record. @yield [Array] record data @return [self] self. If block is not specified, this returns Enumerator.
Source
# File lib/mysql/result.rb, line 94 def each_hash(**opts, &block) @index = 0 return enum_for(:each_hash, **opts) unless block while (rec = fetch_hash(**opts)) block.call rec end self end
Iterate block with record as Hash. @param [Boolean] with_table if true, hash key is “table_name.field_name”. @yield [Hash] record data @return [self] self. If block is not specified, this returns Enumerator.
Source
# File lib/mysql/result.rb, line 44 def fetch(**) if @records && @index < @records.size @records[@index] = @records[@index].to_a unless @records[@index].is_a? Array @index += 1 return @records[@index-1] end rec = @protocol.retr_record(@record_class)&.to_a return nil unless rec @records[@index] = rec if @records @index += 1 @size += 1 return rec end
@return [Array] current record data
Source
# File lib/mysql/result.rb, line 63 def fetch_hash(**opts) row = fetch(**opts) return nil unless row with_table = @opts.merge(opts)[:with_table] if with_table and @fieldname_with_table.nil? @fieldname_with_table = @fields.map{|f| [f.table, f.name].join(".")} end ret = {} @fields.each_index do |i| fname = with_table ? @fieldname_with_table[i] : @fields[i].name ret[fname] = row[i] end ret end
Return data of current record as Hash. The hash key is field name. @param [Boolean] with_table if true, hash key is “table_name.field_name”. @return [Hash] current record data
Source
# File lib/mysql/result.rb, line 27 def retrieve @records = @protocol.retr_all_records(@record_class) @size = @records.size end
Source
# File lib/mysql/result.rb, line 119 def row_seek(n) ret = @index @index = n ret end
Set current position of record @param [Integer] n record index @return [Integer] previous position
Source
# File lib/mysql/result.rb, line 112 def row_tell @index end
@return [Integer] current record position
Source
# File lib/mysql/result.rb, line 127 def server_status @protocol.server_status end
Server status value @return [Integer] server status value