Class: Mysql::Result
- Inherits:
-
Object
- Object
- Mysql::Result
- Includes:
- Enumerable
- Defined in:
- lib/ffi-mysql/result.rb
Overview
Result set.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.finalizer(result) ⇒ Object
Internal finalizer, calls self.free.
Instance Method Summary collapse
-
#data_seek(row) ⇒ self
Seeks to an arbitrary row in the result set.
-
#each {|Array<String>| ... } ⇒ Object
Iterates over all rows in this result set.
-
#each_hash(with_table = false) {|Hash| ... } ⇒ Object
Iterates over all rows in this result set.
-
#fetch_field ⇒ Field?
The information for the next Field or nil.
-
#fetch_field_direct(fieldnr) ⇒ Field
The information for field of column fieldnr.
-
#fetch_fields ⇒ Array<Field>
Array of field-informations for each column in this result set.
-
#fetch_hash(with_table = false) ⇒ Hash
Hash of elements “field” => “value”.
-
#fetch_lengths ⇒ Array<Integer>
The array of number of chars for each column.
-
#fetch_row ⇒ Array<String>
Ary of elements of the next row.
-
#field_seek(offset) ⇒ Integer
Sets the field cursor to the given offset.
-
#field_tell ⇒ Integer
The position of the field cursor after the last fetch_field.
-
#free ⇒ Object
Frees the result object.
-
#initialize(mysql, result) ⇒ Result
constructor
Create the next result object.
-
#num_fields ⇒ Integer
The number of columns in this result set.
-
#num_rows ⇒ Integer
The number of rows in this result set.
-
#row_seek(offset) ⇒ Integer
Sets the position of the row cursor.
-
#row_tell ⇒ Integer
The current position of the row cursor.
Constructor Details
#initialize(mysql, result) ⇒ Result
Create the next result object.
10 11 12 13 14 15 16 |
# File 'lib/ffi-mysql/result.rb', line 10 def initialize( mysql, result ) @mysql = mysql @result = result @num_rows = @num_fields = nil raise ArgumentError, "Invalid result object" if @result.nil? or @result.null? ObjectSpace.define_finalizer( self, Result.finalizer(@result) ) end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/ffi-mysql/result.rb', line 7 def fields @fields end |
Class Method Details
.finalizer(result) ⇒ Object
Internal finalizer, calls self.free.
162 163 164 165 166 |
# File 'lib/ffi-mysql/result.rb', line 162 def self.finalizer(result) Proc.new do |*args| C::mysql_free_result(result) end end |
Instance Method Details
#data_seek(row) ⇒ self
Seeks to an arbitrary row in the result set.
155 156 157 158 159 |
# File 'lib/ffi-mysql/result.rb', line 155 def data_seek( row ) raise Error, "Result has been freed" unless @result C::mysql_data_seek(@result, row) self end |
#each {|Array<String>| ... } ⇒ Object
Iterates over all rows in this result set.
63 64 65 66 67 |
# File 'lib/ffi-mysql/result.rb', line 63 def each while row = fetch_row yield row end end |
#each_hash(with_table = false) {|Hash| ... } ⇒ Object
Iterates over all rows in this result set.
104 105 106 107 108 |
# File 'lib/ffi-mysql/result.rb', line 104 def each_hash(with_table = false) while row = fetch_hash(with_table) yield row end end |
#fetch_field ⇒ Field?
Returns the information for the next Field or nil.
125 126 127 128 129 130 131 132 133 |
# File 'lib/ffi-mysql/result.rb', line 125 def fetch_field raise Error, "Result has been freed" unless @result field_ptr = C::mysql_fetch_field(@result) if field_ptr.nil? or field_ptr.null? nil else Field.new(C::Field.new(field_ptr)) end end |
#fetch_field_direct(fieldnr) ⇒ Field
Returns the information for field of column fieldnr.
137 138 139 140 141 142 143 |
# File 'lib/ffi-mysql/result.rb', line 137 def fetch_field_direct( fieldnr ) raise Error, "Result has been freed" unless @result n = num_fields raise Error, "#{fieldnr}: out of range (max: #{n})" if fieldnr < 0 or fieldnr >= n field_ptr = C::mysql_fetch_field_direct(@result, fieldnr) Field.new(C::Field.new(field_ptr)) end |
#fetch_fields ⇒ Array<Field>
Returns array of field-informations for each column in this result set.
146 147 148 149 150 |
# File 'lib/ffi-mysql/result.rb', line 146 def fetch_fields raise Error, "Result has been freed" unless @result n = num_fields (0...n).map{|i| fetch_field_direct(i)} end |
#fetch_hash(with_table = false) ⇒ Hash
Returns hash of elements “field” => “value”.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ffi-mysql/result.rb', line 85 def fetch_hash(with_table = false) return nil unless row = fetch_row keys = if with_table @tblcolnames ||= fetch_fields.map{|f| "#{f.table}.#{f.name}"} else @colnames ||= fetch_fields.map{|f| f.name} end hash = {} row.each_with_index do |value, i| hash[keys[i]] = value end hash end |
#fetch_lengths ⇒ Array<Integer>
Returns the array of number of chars for each column.
38 39 40 41 42 |
# File 'lib/ffi-mysql/result.rb', line 38 def fetch_lengths raise Error, "Result has been freed" unless @result lengths = C::mysql_fetch_lengths(@result) (lengths.nil? or lengths.null?) ? nil : lengths.read_array_of_long(num_fields) end |
#fetch_row ⇒ Array<String>
Returns Ary of elements of the next row.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ffi-mysql/result.rb', line 45 def fetch_row raise Error, "Result has been freed" unless @result row = C::mysql_fetch_row(@result) if row.nil? or row.null? nil else lengths = fetch_lengths ptrs = row.get_array_of_pointer(0, lengths.size) (0...lengths.size).map {|i| ptr = ptrs[i] ptr.nil? or ptr.null? ? nil : ptr.read_string(lengths[i]) } end end |
#field_seek(offset) ⇒ Integer
Sets the field cursor to the given offset.
119 120 121 122 |
# File 'lib/ffi-mysql/result.rb', line 119 def field_seek(offset) raise Error, "Result has been freed" unless @result C::mysql_field_seek(@result, offset) end |
#field_tell ⇒ Integer
Returns The position of the field cursor after the last fetch_field.
111 112 113 114 |
# File 'lib/ffi-mysql/result.rb', line 111 def field_tell raise Error, "Result has been freed" unless @result C::mysql_field_tell(@result) end |
#free ⇒ Object
Frees the result object.
19 20 21 22 23 |
# File 'lib/ffi-mysql/result.rb', line 19 def free C::mysql_free_result(@result) @result = nil ObjectSpace.undefine_finalizer( self ) end |
#num_fields ⇒ Integer
Returns the number of columns in this result set.
32 33 34 35 |
# File 'lib/ffi-mysql/result.rb', line 32 def num_fields raise Error, "Result has been freed" unless @result @num_fields ||= C::mysql_num_fields(@result) end |
#num_rows ⇒ Integer
Returns the number of rows in this result set.
26 27 28 29 |
# File 'lib/ffi-mysql/result.rb', line 26 def num_rows raise Error, "Result has been freed" unless @result @num_rows ||= C::mysql_num_rows(@result) end |