Class: MysqlPR::ResultBase
- Inherits:
-
Object
- Object
- MysqlPR::ResultBase
- Includes:
- Enumerable
- Defined in:
- lib/mysql-pr.rb
Overview
Result set base class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Array<MysqlPR::Field>
readonly
Field list.
Instance Method Summary collapse
-
#data_seek(n) ⇒ self
Set record position.
-
#each {|Array| ... } ⇒ self, Enumerator
Iterate block with record.
-
#each_hash(with_table = nil) {|Hash| ... } ⇒ self, Enumerator
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
-
#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 records.
Constructor Details
#initialize(fields) ⇒ ResultBase
Returns a new instance of ResultBase.
648 649 650 651 652 653 654 655 |
# File 'lib/mysql-pr.rb', line 648 def initialize(fields) @fields = fields @field_index = 0 @records = [] @index = 0 @fieldname_with_table = nil @fetched_record = nil end |
Instance Attribute Details
#fields ⇒ Array<MysqlPR::Field> (readonly)
Returns field list.
645 646 647 |
# File 'lib/mysql-pr.rb', line 645 def fields @fields end |
Instance Method Details
#data_seek(n) ⇒ self
Set record position
722 723 724 725 |
# File 'lib/mysql-pr.rb', line 722 def data_seek(n) @index = n self end |
#each {|Array| ... } ⇒ self, Enumerator
Iterate block with record.
697 698 699 700 701 702 703 704 |
# File 'lib/mysql-pr.rb', line 697 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, Enumerator
Iterate block with record as Hash.
710 711 712 713 714 715 716 717 |
# File 'lib/mysql-pr.rb', line 710 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.
667 668 669 670 671 672 673 674 675 |
# File 'lib/mysql-pr.rb', line 667 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 @fetched_record end |
#fetch_hash(with_table = nil) ⇒ Hash?
Return data of current record as Hash.
681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/mysql-pr.rb', line 681 def fetch_hash(with_table = nil) row = fetch return nil unless row @fieldname_with_table = @fields.map { |f| "#{f.table}.#{f.name}" } if with_table && @fieldname_with_table.nil? 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.
658 |
# File 'lib/mysql-pr.rb', line 658 def free; end |
#row_seek(n) ⇒ Integer
Set current position of record
735 736 737 738 739 |
# File 'lib/mysql-pr.rb', line 735 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
728 729 730 |
# File 'lib/mysql-pr.rb', line 728 def row_tell @index end |
#size ⇒ Integer Also known as: num_rows
Returns number of records.
661 662 663 |
# File 'lib/mysql-pr.rb', line 661 def size @records.size end |