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
-
#size ⇒ Integer
(also: #num_rows, #count)
readonly
Number of record.
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.
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 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
#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 |
#size ⇒ Integer (readonly) Also known as: num_rows, count
Returns 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
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.
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.
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 |
#fetch ⇒ Array Also known as: fetch_row
Returns 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.
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 |
#free ⇒ void
This method returns an undefined value.
ignore
34 35 36 |
# File 'lib/mysql/result.rb', line 34 def free # dummy end |
#retrieve ⇒ Object
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
119 120 121 122 123 |
# File 'lib/mysql/result.rb', line 119 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
112 113 114 |
# File 'lib/mysql/result.rb', line 112 def row_tell @index end |
#server_status ⇒ Integer
Server status value
127 128 129 |
# File 'lib/mysql/result.rb', line 127 def server_status @protocol.server_status end |