class Result
A result set from a database query with row iteration and type casting.
Definitions
def initialize(connection, types = {}, address)
Initialize a new result set wrapper.
Signature
-
parameter
connectionConnection The connection that produced this result.
-
parameter
typesHash Type mapping for field conversion.
-
parameter
addressFFI::Pointer The pointer to the native result.
Implementation
def initialize(connection, types = {}, address)
super(address)
@connection = connection
@fields = nil
@types = types
@casts = nil
end
def field_count
Get the number of fields in this result set.
Signature
-
returns
Integer The field count.
Implementation
def field_count
Native.mysql_num_fields(self)
end
def fields
Get the field metadata for this result set.
Signature
-
returns
Array(Field) The array of field objects.
Implementation
def fields
unless @fields
pointer = Native.mysql_fetch_fields(self)
@fields = field_count.times.map do |index|
Field.new(pointer + index * Field.size)
end
end
return @fields
end
def field_names
Get the field names for this result set.
Signature
-
returns
Array(String) The array of field names.
Implementation
def field_names
fields.map(&:name)
end
def field_types
Get the type converters for each field.
Signature
-
returns
Array The array of type converter objects.
Implementation
def field_types
fields.map{|field| @types[field.type]}
end
def row_count
Get the number of rows in this result set. In the context of unbuffered queries, this is the number of rows that have been fetched so far.
Signature
-
returns
Integer The row count.
Implementation
def row_count
Native.mysql_num_rows(self)
end
def cast!(row)
Cast row values to appropriate Ruby types.
Signature
-
parameter
rowArray The raw row data.
-
returns
Array The row with values cast to proper types.
Implementation
def cast!(row)
@casts ||= self.field_types
row.size.times do |index|
if cast = @casts[index]
row[index] = cast.parse(row[index])
end
end
return row
end
def each
Iterate over each row in the result set.
Signature
-
yields
{|row| ...} Each row as an array.
-
parameter
rowArray The current row data.
-
parameter
Implementation
def each
row = FFI::MemoryPointer.new(:pointer)
field_count = self.field_count
while true
status = Native.mysql_fetch_row_start(row, self)
while status != 0
@connection.wait_for(status)
status = Native.mysql_fetch_row_cont(row, self, status)
end
pointer = row.read_pointer
if pointer.null?
break
else
yield cast!(pointer.get_array_of_string(0, field_count))
end
end
@connection.check_error!("Reading recordset")
end
def map(&block)
Map over each row in the result set.
Signature
-
yields
{|row| ...} Each row as an array.
-
parameter
rowArray The current row data.
-
parameter
-
returns
Array The mapped results.
Implementation
def map(&block)
results = []
self.each do |row|
results << yield(row)
end
return results
end
def to_a
Convert the entire result set to an array.
Signature
-
returns
Array(Array) All rows as arrays.
Implementation
def to_a
rows = []
self.each do |row|
rows << row
end
return rows
end