Class: Mysql::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ffi-mysql/result.rb

Overview

Result set.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mysql, result) ⇒ Result

Create the next result object.

Raises:

  • (ArgumentError)


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

#fieldsObject (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.

Parameters:

  • row (Integer)

    the number of row to use next

Returns:

  • (self)

Raises:



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.

Yields:

  • (Array<String>)

    Called once for each row in this result set

See Also:



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.

Parameters:

  • with_table (Boolean) (defaults to: false)

    if true, fields are denoted with table “tablename.fieldname”

Yields:

  • (Hash)

    Called once for each row in this result set with a row-hash

See Also:



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_fieldField?

Returns the information for the next Field or nil.

Returns:

  • (Field, nil)

    the information for the next Field or nil

Raises:



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.

Parameters:

  • fieldnr (Integer)

    column number

Returns:

  • (Field)

    the information for field of column fieldnr

Raises:



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_fieldsArray<Field>

Returns array of field-informations for each column in this result set.

Returns:

  • (Array<Field>)

    array of field-informations for each column in this result set

Raises:



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”.

Parameters:

  • with_table (Boolean) (defaults to: false)

    if true, fields are denoted with table “tablename.fieldname”

Returns:

  • (Hash)

    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_lengthsArray<Integer>

Returns the array of number of chars for each column.

Returns:

  • (Array<Integer>)

    the array of number of chars for each column

Raises:



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_rowArray<String>

Returns Ary of elements of the next row.

Returns:

  • (Array<String>)

    Ary of elements of the next row.

Raises:



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.

Parameters:

  • the (Integer)

    new field offset

Returns:

  • (Integer)

    the position of the previous field cursor

Raises:



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_tellInteger

Returns The position of the field cursor after the last fetch_field.

Returns:

  • (Integer)

    The position of the field cursor after the last fetch_field.

Raises:



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

#freeObject

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_fieldsInteger

Returns the number of columns in this result set.

Returns:

  • (Integer)

    the number of columns in this result set

Raises:



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_rowsInteger

Returns the number of rows in this result set.

Returns:

  • (Integer)

    the number of rows in this result set

Raises:



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

#row_seek(offset) ⇒ Integer

Sets the position of the row cursor.

Parameters:

  • offset (Integer)

    the new position of the row cursor

Returns:

  • (Integer)

    the former position of the row cursor

Raises:



78
79
80
81
# File 'lib/ffi-mysql/result.rb', line 78

def row_seek( offset )
    raise Error, "Result has been freed" unless @result
    C::mysql_row_seek(@result, offset)
end

#row_tellInteger

Returns the current position of the row cursor.

Returns:

  • (Integer)

    the current position of the row cursor

Raises:



70
71
72
73
# File 'lib/ffi-mysql/result.rb', line 70

def row_tell
    raise Error, "Result has been freed" unless @result
    C::mysql_row_tell(@result)
end