Class: Mysql::ResultBase

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mysql/result.rb

Overview

Result set

Direct Known Subclasses

Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields, protocol, record_class, **opts) ⇒ ResultBase

Returns a new instance of ResultBase.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#fieldsArray<Mysql::Field> (readonly) Also known as: fetch_fields

Returns field list.

Returns:



8
9
10
# File 'lib/mysql/result.rb', line 8

def fields
  @fields
end

#resultMysql::StatementResult (readonly)

Returns:

  • (Mysql::StatementResult)


12
13
14
# File 'lib/mysql/result.rb', line 12

def result
  @result
end

#sizeInteger (readonly) Also known as: num_rows, count

Returns number of record.

Returns:

  • (Integer)

    number of record



39
40
41
# File 'lib/mysql/result.rb', line 39

def size
  @size
end

Instance Method Details

#data_seek(n) ⇒ self

Set record position

Parameters:

  • n (Integer)

    record index

Returns:

  • (self)

    self



106
107
108
109
# File 'lib/mysql/result.rb', line 106

def data_seek(n)
  @index = n
  self
end

#each(**opts) {|Array| ... } ⇒ self

Iterate block with record.

Yields:

  • (Array)

    record data

Returns:

  • (self)

    self. If block is not specified, this returns Enumerator.



81
82
83
84
85
86
87
88
# 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

#each_hash(**opts) {|Hash| ... } ⇒ self

Iterate block with record as Hash.

Parameters:

  • with_table (Boolean)

    if true, hash key is “table_name.field_name”.

Yields:

  • (Hash)

    record data

Returns:

  • (self)

    self. If block is not specified, this returns Enumerator.



94
95
96
97
98
99
100
101
# 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

#fetchArray Also known as: fetch_row

Returns current record data.

Returns:

  • (Array)

    current record data



44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

#fetch_hash(**opts) ⇒ Hash

Return data of current record as Hash. The hash key is field name.

Parameters:

  • with_table (Boolean)

    if true, hash key is “table_name.field_name”.

Returns:

  • (Hash)

    current record data



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#freevoid

This method returns an undefined value.

ignore



34
35
36
# File 'lib/mysql/result.rb', line 34

def free
  # dummy
end

#retrieveObject



27
28
29
30
# File 'lib/mysql/result.rb', line 27

def retrieve
  @records = @protocol.retr_all_records(@record_class)
  @size = @records.size
end

#row_seek(n) ⇒ Integer

Set current position of record

Parameters:

  • n (Integer)

    record index

Returns:

  • (Integer)

    previous position



119
120
121
122
123
# File 'lib/mysql/result.rb', line 119

def row_seek(n)
  ret = @index
  @index = n
  ret
end

#row_tellInteger

Returns current record position.

Returns:

  • (Integer)

    current record position



112
113
114
# File 'lib/mysql/result.rb', line 112

def row_tell
  @index
end

#server_statusInteger

Server status value

Returns:

  • (Integer)

    server status value



127
128
129
# File 'lib/mysql/result.rb', line 127

def server_status
  @protocol.server_status
end