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.field_count(self)
end
def field_types
Get the type converters for each field.
Signature
-
returns
Array The array of type converter objects.
Implementation
def field_types
field_count.times.collect{|i| @types[Native.field_type(self, i)]}
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
field_count.times.collect{|i| Native.field_name(self, i)}
end
def row_count
Get the number of rows in this result set.
Signature
-
returns
Integer The row count.
Implementation
def row_count
Native.row_count(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_count.times do |i|
yield cast!(get_row(i))
end
Native.clear(self)
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