Class: Mysql::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mysql.rb,
lib/mysql/compat.rb

Overview

Result set

Direct Known Subclasses

SimpleQueryResult, StatementResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Result

Returns a new instance of Result.



473
474
475
476
477
478
# File 'lib/mysql.rb', line 473

def initialize(mysql, fields)
  @fields = fields
  @fieldname_with_table = nil
  @index = 0
  @records = recv_all_records mysql.protocol, fields, mysql.charset
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



471
472
473
# File 'lib/mysql.rb', line 471

def fields
  @fields
end

Instance Method Details

#data_seek(n) ⇒ Object



142
143
144
# File 'lib/mysql/compat.rb', line 142

def data_seek(n)
  @index = n
end

#each(&block) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/mysql.rb', line 507

def each(&block)
  return enum_for(:each) unless block
  while rec = fetch_row
    block.call rec
  end
  self
end

#each_hash(with_table = nil, &block) ⇒ Object



515
516
517
518
519
520
521
# File 'lib/mysql.rb', line 515

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

#fetchObject



491
492
493
494
495
496
# File 'lib/mysql.rb', line 491

def fetch_row
  return nil if @index >= @records.size
  rec = @records[@index]
  @index += 1
  return rec
end

#fetch_fieldObject



165
166
167
168
169
170
# File 'lib/mysql/compat.rb', line 165

def fetch_field
  return nil if @field_index >= @fields.length
  ret = @fields[@field_index]
  @field_index += 1
  ret
end

#fetch_field_direct(n) ⇒ Object

Raises:



180
181
182
183
# File 'lib/mysql/compat.rb', line 180

def fetch_field_direct(n)
  raise ClientError, "invalid argument: #{n}" if n < 0 or n >= @fields.length
  @fields[n]
end

#fetch_fieldsObject



185
186
187
# File 'lib/mysql/compat.rb', line 185

def fetch_fields
  @fields
end

#fetch_hash(with_table = nil) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/mysql.rb', line 493

def fetch_hash(with_table=nil)
  row = fetch_row
  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

#fetch_lengthsObject



189
190
191
192
# File 'lib/mysql/compat.rb', line 189

def fetch_lengths
  return nil unless @fetched_record
  @fetched_record.map{|c|c.nil? ? 0 : c.length}
end

#fetch_rowObject



484
485
486
487
488
489
# File 'lib/mysql.rb', line 484

def fetch_row
  return nil if @index >= @records.size
  rec = @records[@index]
  @index += 1
  return rec
end

#fetch_row_origObject



160
161
162
163
164
165
# File 'lib/mysql/compat.rb', line 160

def fetch_row
  return nil if @index >= @records.size
  rec = @records[@index]
  @index += 1
  return rec
end

#field_seek(n) ⇒ Object



176
177
178
# File 'lib/mysql/compat.rb', line 176

def field_seek(n)
  @field_index = n
end

#field_tellObject



172
173
174
# File 'lib/mysql/compat.rb', line 172

def field_tell
  @field_index
end

#freeObject



156
157
158
# File 'lib/mysql/compat.rb', line 156

def free
  # do nothing
end

#initialize_origResult

Returns a new instance of Result.

Returns:

  • (Result)

    a new instance of Result



132
133
134
135
136
137
# File 'lib/mysql/compat.rb', line 132

def initialize(mysql, fields)
  @fields = fields
  @fieldname_with_table = nil
  @index = 0
  @records = recv_all_records mysql.protocol, fields, mysql.charset
end

#num_fieldsObject



194
195
196
# File 'lib/mysql/compat.rb', line 194

def num_fields
  @fields.length
end

#num_rowsObject



138
139
140
# File 'lib/mysql/compat.rb', line 138

def num_rows
  @records.length
end

#row_seek(n) ⇒ Object



150
151
152
153
154
# File 'lib/mysql/compat.rb', line 150

def row_seek(n)
  ret = @index
  @index = n
  ret
end

#row_tellObject



146
147
148
# File 'lib/mysql/compat.rb', line 146

def row_tell
  @index
end

#sizeObject



480
481
482
# File 'lib/mysql.rb', line 480

def size
  @records.size
end