Class: MysqlPR::ResultBase
- Inherits:
-
Object
- Object
- MysqlPR::ResultBase
- Includes:
- Enumerable
- Defined in:
- lib/mysql-pr.rb
Overview
Result set
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
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.
615 616 617 618 619 620 621 622 |
# File 'lib/mysql-pr.rb', line 615 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<MysqlPR::Field> (readonly)
Returns field list.
612 613 614 |
# File 'lib/mysql-pr.rb', line 612 def fields @fields end |
Instance Method Details
#data_seek(n) ⇒ self
Set record position
690 691 692 693 |
# File 'lib/mysql-pr.rb', line 690 def data_seek(n) @index = n self end |
#each {|Array| ... } ⇒ self
Iterate block with record.
667 668 669 670 671 672 673 |
# File 'lib/mysql-pr.rb', line 667 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.
679 680 681 682 683 684 685 |
# File 'lib/mysql-pr.rb', line 679 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.
636 637 638 639 640 641 642 643 |
# File 'lib/mysql-pr.rb', line 636 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.
650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/mysql-pr.rb', line 650 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
626 627 |
# File 'lib/mysql-pr.rb', line 626 def free end |
#row_seek(n) ⇒ Integer
Set current position of record
703 704 705 706 707 |
# File 'lib/mysql-pr.rb', line 703 def row_seek(n) ret = @index @index = n ret end |
#row_tell ⇒ Integer
Returns current record position.
696 697 698 |
# File 'lib/mysql-pr.rb', line 696 def row_tell @index end |
#size ⇒ Integer Also known as: num_rows
Returns number of record.
630 631 632 |
# File 'lib/mysql-pr.rb', line 630 def size @records.size end |