Class: Mysql::ResultBase
- Includes:
- Enumerable
- Defined in:
- lib/vendor/mysql.rb
Overview
Result set
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Array<Mysql::Field>
readonly
Field list.
Instance Method Summary collapse
-
#data_seek(n) ⇒ self
Set record position.
-
#each {|Array| ... } ⇒ self
Iterate block with record.
-
#each_hash(with_table = nil) {|Hash| ... } ⇒ self
Iterate block with record as Hash.
-
#fetch ⇒ Array
(also: #fetch_row)
Current record data.
-
#fetch_hash(with_table = nil) ⇒ Hash
Return data of current record as Hash.
-
#free ⇒ void
ignore.
-
#initialize(fields) ⇒ ResultBase
constructor
A new instance of ResultBase.
-
#row_seek(n) ⇒ Integer
Set current position of record.
-
#row_tell ⇒ Integer
Current record position.
-
#size ⇒ Integer
(also: #num_rows)
Number of record.
Methods included from Enumerable
Constructor Details
#initialize(fields) ⇒ ResultBase
Returns a new instance of ResultBase.
609 610 611 612 613 614 615 616 |
# File 'lib/vendor/mysql.rb', line 609 def initialize(fields) @fields = fields @field_index = 0 # index of field @records = [] # all records @index = 0 # index of record @fieldname_with_table = nil @fetched_record = nil end |
Instance Attribute Details
#fields ⇒ Array<Mysql::Field> (readonly)
Returns field list.
606 607 608 |
# File 'lib/vendor/mysql.rb', line 606 def fields @fields end |
Instance Method Details
#data_seek(n) ⇒ self
Set record position
684 685 686 687 |
# File 'lib/vendor/mysql.rb', line 684 def data_seek(n) @index = n self end |
#each {|Array| ... } ⇒ self
Iterate block with record.
661 662 663 664 665 666 667 |
# File 'lib/vendor/mysql.rb', line 661 def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end |
#each_hash(with_table = nil) {|Hash| ... } ⇒ self
Iterate block with record as Hash.
673 674 675 676 677 678 679 |
# File 'lib/vendor/mysql.rb', line 673 def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end |
#fetch ⇒ Array Also known as: fetch_row
Returns current record data.
630 631 632 633 634 635 636 637 |
# File 'lib/vendor/mysql.rb', line 630 def fetch @fetched_record = nil return nil if @index >= @records.size @records[@index] = @records[@index].to_a unless @records[@index].is_a? Array @fetched_record = @records[@index] @index += 1 return @fetched_record end |
#fetch_hash(with_table = nil) ⇒ Hash
Return data of current record as Hash. The hash key is field name.
644 645 646 647 648 649 650 651 652 653 654 655 656 |
# File 'lib/vendor/mysql.rb', line 644 def fetch_hash(with_table=nil) row = fetch return nil unless row 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
620 621 |
# File 'lib/vendor/mysql.rb', line 620 def free end |
#row_seek(n) ⇒ Integer
Set current position of record
697 698 699 700 701 |
# File 'lib/vendor/mysql.rb', line 697 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
690 691 692 |
# File 'lib/vendor/mysql.rb', line 690 def row_tell @index end |
#size ⇒ Integer Also known as: num_rows
Returns number of record.
624 625 626 |
# File 'lib/vendor/mysql.rb', line 624 def size @records.size end |