Class: Mysql::ResultBase
- Inherits:
-
Object
- Object
- Mysql::ResultBase
- Includes:
- Enumerable
- Defined in:
- lib/mysql/result.rb
Overview
Result set
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Array<Mysql::Field>
(also: #fetch_fields)
readonly
Field list.
- #result ⇒ Mysql::StatementResult readonly
Instance Method Summary collapse
-
#data_seek(n) ⇒ self
Set record position.
-
#each(**opts) {|Array| ... } ⇒ self
Iterate block with record.
-
#each_hash(**opts) {|Hash| ... } ⇒ self
Iterate block with record as Hash.
-
#fetch ⇒ Array
(also: #fetch_row)
Current record data.
-
#fetch_hash(**opts) ⇒ Hash
Return data of current record as Hash.
-
#free ⇒ void
ignore.
-
#initialize(fields, protocol, record_class, **opts) ⇒ ResultBase
constructor
A new instance of ResultBase.
- #retrieve ⇒ Object
-
#row_seek(n) ⇒ Integer
Set current position of record.
-
#row_tell ⇒ Integer
Current record position.
-
#server_status ⇒ Integer
Server status value.
-
#size ⇒ Integer
(also: #num_rows)
Number of record.
Constructor Details
#initialize(fields, protocol, record_class, **opts) ⇒ ResultBase
Returns a new instance of ResultBase.
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
#fields ⇒ Array<Mysql::Field> (readonly) Also known as: fetch_fields
Returns field list.
8 9 10 |
# File 'lib/mysql/result.rb', line 8 def fields @fields end |
#result ⇒ Mysql::StatementResult (readonly)
12 13 14 |
# File 'lib/mysql/result.rb', line 12 def result @result end |
Instance Method Details
#data_seek(n) ⇒ self
Set record position
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.
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.
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 |
#fetch ⇒ Array Also known as: fetch_row
Returns 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.
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 |
#free ⇒ void
This method returns an undefined value.
ignore
32 33 34 |
# File 'lib/mysql/result.rb', line 32 def free # dummy end |
#retrieve ⇒ Object
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
117 118 119 120 121 |
# File 'lib/mysql/result.rb', line 117 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
110 111 112 |
# File 'lib/mysql/result.rb', line 110 def row_tell @index end |
#server_status ⇒ Integer
Server status value
125 126 127 |
# File 'lib/mysql/result.rb', line 125 def server_status @protocol.server_status end |
#size ⇒ Integer Also known as: num_rows
Returns number of record.
37 38 39 |
# File 'lib/mysql/result.rb', line 37 def size @records.size end |