Class: DB::MariaDB::Native::Result
- Inherits:
-
FFI::Pointer
- Object
- FFI::Pointer
- DB::MariaDB::Native::Result
- Defined in:
- lib/db/mariadb/native/result.rb
Overview
A result set from a database query with row iteration and type casting.
Instance Method Summary collapse
-
#cast!(row) ⇒ Object
Cast row values to appropriate Ruby types.
-
#each ⇒ Object
Iterate over each row in the result set.
-
#field_count ⇒ Object
Get the number of fields in this result set.
-
#field_names ⇒ Object
(also: #keys)
Get the field names for this result set.
-
#field_types ⇒ Object
Get the type converters for each field.
-
#fields ⇒ Object
Get the field metadata for this result set.
-
#initialize(connection, types = {}, address) ⇒ Result
constructor
Initialize a new result set wrapper.
-
#map(&block) ⇒ Object
Map over each row in the result set.
-
#row_count ⇒ Object
(also: #count)
Get the number of rows in this result set.
-
#to_a ⇒ Object
Convert the entire result set to an array.
Constructor Details
#initialize(connection, types = {}, address) ⇒ Result
Initialize a new result set wrapper.
25 26 27 28 29 30 31 32 |
# File 'lib/db/mariadb/native/result.rb', line 25 def initialize(connection, types = {}, address) super(address) @connection = connection @fields = nil @types = types @casts = nil end |
Instance Method Details
#cast!(row) ⇒ Object
Cast row values to appropriate Ruby types.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/db/mariadb/native/result.rb', line 79 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 |
#each ⇒ Object
Iterate over each row in the result set.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/db/mariadb/native/result.rb', line 94 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 |
#field_count ⇒ Object
Get the number of fields in this result set.
36 37 38 |
# File 'lib/db/mariadb/native/result.rb', line 36 def field_count Native.mysql_num_fields(self) end |
#field_names ⇒ Object Also known as: keys
Get the field names for this result set.
56 57 58 |
# File 'lib/db/mariadb/native/result.rb', line 56 def field_names fields.map(&:name) end |
#field_types ⇒ Object
Get the type converters for each field.
62 63 64 |
# File 'lib/db/mariadb/native/result.rb', line 62 def field_types fields.map{|field| @types[field.type]} end |
#fields ⇒ Object
Get the field metadata for this result set.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/db/mariadb/native/result.rb', line 42 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 |
#map(&block) ⇒ Object
Map over each row in the result set.
123 124 125 126 127 128 129 130 131 |
# File 'lib/db/mariadb/native/result.rb', line 123 def map(&block) results = [] self.each do |row| results << yield(row) end return results end |
#row_count ⇒ Object Also known as: 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.
69 70 71 |
# File 'lib/db/mariadb/native/result.rb', line 69 def row_count Native.mysql_num_rows(self) end |
#to_a ⇒ Object
Convert the entire result set to an array.
135 136 137 138 139 140 141 142 143 |
# File 'lib/db/mariadb/native/result.rb', line 135 def to_a rows = [] self.each do |row| rows << row end return rows end |