Class: Mysql::ResultBase
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.
Constructor Details
#initialize(fields) ⇒ ResultBase
Returns a new instance of ResultBase.
620 621 622 623 624 625 626 627 |
# File 'lib/mysql.rb', line 620 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.
617 618 619 |
# File 'lib/mysql.rb', line 617 def fields @fields end |
Instance Method Details
#data_seek(n) ⇒ self
Set record position
695 696 697 698 |
# File 'lib/mysql.rb', line 695 def data_seek(n) @index = n self end |
#each {|Array| ... } ⇒ self
Iterate block with record.
672 673 674 675 676 677 678 |
# File 'lib/mysql.rb', line 672 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.
684 685 686 687 688 689 690 |
# File 'lib/mysql.rb', line 684 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.
641 642 643 644 645 646 647 648 |
# File 'lib/mysql.rb', line 641 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.
655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/mysql.rb', line 655 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
631 632 |
# File 'lib/mysql.rb', line 631 def free end |
#row_seek(n) ⇒ Integer
Set current position of record
708 709 710 711 712 |
# File 'lib/mysql.rb', line 708 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
701 702 703 |
# File 'lib/mysql.rb', line 701 def row_tell @index end |
#size ⇒ Integer Also known as: num_rows
Returns number of record.
635 636 637 |
# File 'lib/mysql.rb', line 635 def size @records.size end |