Class: DB::MariaDB::Native::Result
- Inherits:
-
FFI::Pointer
- Object
- FFI::Pointer
- DB::MariaDB::Native::Result
- Defined in:
- lib/db/mariadb/native/result.rb
Instance Method Summary collapse
- #cast!(row) ⇒ Object
- #each ⇒ Object
- #field_count ⇒ Object
- #field_names ⇒ Object (also: #keys)
- #field_types ⇒ Object
- #fields ⇒ Object
-
#initialize(connection, types = {}, address) ⇒ Result
constructor
A new instance of Result.
- #row_count ⇒ Object (also: #count)
- #to_a ⇒ Object
Constructor Details
#initialize(connection, types = {}, address) ⇒ Result
Returns a new instance of Result.
35 36 37 38 39 40 41 42 |
# File 'lib/db/mariadb/native/result.rb', line 35 def initialize(connection, types = {}, address) super(address) @connection = connection @fields = nil @types = types @casts = nil end |
Instance Method Details
#cast!(row) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/db/mariadb/native/result.rb', line 75 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/db/mariadb/native/result.rb', line 87 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
44 45 46 |
# File 'lib/db/mariadb/native/result.rb', line 44 def field_count Native.mysql_num_fields(self) end |
#field_names ⇒ Object Also known as: keys
60 61 62 |
# File 'lib/db/mariadb/native/result.rb', line 60 def field_names fields.map(&:name) end |
#field_types ⇒ Object
64 65 66 |
# File 'lib/db/mariadb/native/result.rb', line 64 def field_types fields.map{|field| @types[field.type]} end |
#fields ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/db/mariadb/native/result.rb', line 48 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 |
#row_count ⇒ Object Also known as: count
68 69 70 |
# File 'lib/db/mariadb/native/result.rb', line 68 def row_count Native.mysql_num_rows(self) end |
#to_a ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/db/mariadb/native/result.rb', line 112 def to_a rows = [] self.each do |row| rows << row end return rows end |