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
# File 'lib/mysql/result.rb', line 15

def initialize(fields, protocol, record_class, **opts)
  @fields = fields
  @field_index = 0             # index of field
  @records = []                # all records
  @index = 0                   # index of record
  @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

Instance Method Details

#data_seek(n) ⇒ self

Set record position

Parameters:

  • n (Integer)

    record index

Returns:

  • (self)

    self



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

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.



79
80
81
82
83
84
85
86
# File 'lib/mysql/result.rb', line 79

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.



92
93
94
95
96
97
98
99
# File 'lib/mysql/result.rb', line 92

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



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mysql/result.rb', line 43

def fetch(**)
  if @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
  @index += 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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mysql/result.rb', line 61

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



32
33
34
# File 'lib/mysql/result.rb', line 32

def free
  # dummy
end

#retrieveObject



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

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

#row_seek(n) ⇒ Integer

Set current position of record

Parameters:

  • n (Integer)

    record index

Returns:

  • (Integer)

    previous position



117
118
119
120
121
# File 'lib/mysql/result.rb', line 117

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

#row_tellInteger

Returns current record position.

Returns:

  • (Integer)

    current record position



110
111
112
# File 'lib/mysql/result.rb', line 110

def row_tell
  @index
end

#server_statusInteger

Server status value

Returns:

  • (Integer)

    server status value



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

def server_status
  @protocol.server_status
end

#sizeInteger Also known as: num_rows

Returns number of record.

Returns:

  • (Integer)

    number of record



37
38
39
# File 'lib/mysql/result.rb', line 37

def size
  @records.size
end