Class: Mysql2::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mysql2/result.rb

Overview

Mysql2::Result

Constant Summary collapse

FIELD_TYPE_STRING =
{
  Mysql::Field::TYPE_DECIMAL     => "decimal",
  Mysql::Field::TYPE_TINY        => "tinyint",
  Mysql::Field::TYPE_SHORT       => "smallint",
  Mysql::Field::TYPE_LONG        => "int",
  Mysql::Field::TYPE_FLOAT       => "float",
  Mysql::Field::TYPE_DOUBLE      => "double",
  Mysql::Field::TYPE_NULL        => "null",
  Mysql::Field::TYPE_TIMESTAMP   => "timestamp",
  Mysql::Field::TYPE_LONGLONG    => "bigint",
  Mysql::Field::TYPE_INT24       => "mediumint",
  Mysql::Field::TYPE_DATE        => "date",
  Mysql::Field::TYPE_TIME        => "time",
  Mysql::Field::TYPE_DATETIME    => "datetime",
  Mysql::Field::TYPE_YEAR        => "year",
  Mysql::Field::TYPE_NEWDATE     => "date",
  Mysql::Field::TYPE_VARCHAR     => "varchar",
  Mysql::Field::TYPE_BIT         => "bit",
  Mysql::Field::TYPE_TIMESTAMP2  => "timestamp",
  Mysql::Field::TYPE_DATETIME2   => "datetime",
  Mysql::Field::TYPE_TIME2       => "time",
  Mysql::Field::TYPE_TYPED_ARRAY => "typed_array",
  Mysql::Field::TYPE_INVALID     => "invalid",
  Mysql::Field::TYPE_BOOL        => "bool",
  Mysql::Field::TYPE_JSON        => "json",
  Mysql::Field::TYPE_NEWDECIMAL  => "decimal",
  Mysql::Field::TYPE_ENUM        => "enum",
  Mysql::Field::TYPE_SET         => "set",
  Mysql::Field::TYPE_TINY_BLOB   => "tinyblob",
  Mysql::Field::TYPE_MEDIUM_BLOB => "mediumblob",
  Mysql::Field::TYPE_LONG_BLOB   => "longblob",
  Mysql::Field::TYPE_BLOB        => "blob",
  Mysql::Field::TYPE_VAR_STRING  => "varchar",
  Mysql::Field::TYPE_STRING      => "char",
  Mysql::Field::TYPE_GEOMETRY    => "geometry",
  Mysql::Field::TYPE_CHAR        => "tinyint",
  Mysql::Field::TYPE_INTERVAL    => "interval",
}.freeze
BINARY_CHARSET =
63

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, options) ⇒ Result

Returns a new instance of Result.

Parameters:

  • result (Mysql::Result)
  • options (Hash)


10
11
12
13
14
15
16
17
18
19
# File 'lib/mysql2/result.rb', line 10

def initialize(result, options)
  @result = result
  @query_options = options
  server_status = result.server_status
  @server_flags = {
    no_good_index_used: server_status & Mysql::SERVER_QUERY_NO_GOOD_INDEX_USED != 0,
    no_index_used: server_status & Mysql::SERVER_QUERY_NO_INDEX_USED != 0,
    query_was_slow: server_status & Mysql::SERVER_QUERY_WAS_SLOW != 0,
  }
end

Instance Attribute Details

#server_flagsObject (readonly)

Returns the value of attribute server_flags.



4
5
6
# File 'lib/mysql2/result.rb', line 4

def server_flags
  @server_flags
end

Instance Method Details

#convert_type(value, field, opts) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/mysql2/result.rb', line 162

def convert_type(value, field, opts)
  return nil if value.nil?
  case field.type
  when Mysql::Field::TYPE_BIT
    opts[:cast_booleans] ? value != "\x00" : value
  when Mysql::Field::TYPE_TINY
    opts[:cast_booleans] && field.length == 1 ? value != 0 : value
  when Mysql::Field::TYPE_TIME
    Time.new(2000, 1, 1) + value
  else
    value
  end
end

#each(**opts, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mysql2/result.rb', line 38

def each(**opts, &block)
  opts = @query_options.merge(opts)

  raise Mysql2::Error, 'Result set has already been freed' if opts[:stream] && @all_retrieved
  return enum_for(:each, **opts) unless block

  @pos ||= 0
  @cached ||= []
  @pos = 0 unless opts[:stream]

  while @pos < @cached.size
    block.call @cached[@pos]
    @pos += 1
  end

  @result.data_seek @pos
  while (row = @result.fetch(**opts))
    row = @result.fields.map.with_index do |f, i|
      opts[:cast] ? convert_type(row[i], f, opts) : row[i]
    end
    if opts[:as] == :hash
      row = @result.fields.map.with_index.to_h do |f, i|
        key = opts[:symbolize_keys] ? f.name.intern : f.name
        [key, row[i]]
      end
    end
    @cached.push row if opts[:cache_rows]
    @pos += 1
    block.call row
  end
  @all_retrieved = true
  self
rescue Mysql::Error => e
  raise Mysql2::Error, e.message
end

#field_type_string(field) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mysql2/result.rb', line 115

def field_type_string(field)
  if field.type == Mysql::Field::TYPE_STRING
    return 'enum' if field.flags & Mysql::Field::ENUM_FLAG != 0
    return 'set' if field.flags & Mysql::Field::SET_FLAG != 0
  end
  type = FIELD_TYPE_STRING[field.type]
  if type =~ /int$|bit$|year$/
    type += "(#{field.length})"
  elsif type =~ /char$/
    if field.charsetnr == BINARY_CHARSET
      type = type.sub(/char/, 'binary') + "(#{field.length})"
    else
      type += "(#{field.length / 3})"
    end
  elsif type =~ /float|double/
    type += "(#{field.length},#{field.decimals})"
  elsif type == 'decimal'
    type += "(#{field.length - (field.decimals > 0 ? 2 : 1)},#{field.decimals})"
  elsif type == 'blob'
    if field.charsetnr == BINARY_CHARSET
      case field.length
      when 255
        type = 'tinyblob'
      when 65535
        type = 'blob'
      when 16777215
        type = 'mediumblob'
      when 4294967295
        type = 'longblob'
      end
    else
      type = 'text'
      case field.length
      when 255 * 3
        type = 'tinytext'
      when 65535 * 3
        type = 'text'
      when 16777215 * 3
        type = 'mediumtext'
      when 4294967295
        type = 'longtext'
      end
    end
  end
  type
end

#field_typesObject



34
35
36
# File 'lib/mysql2/result.rb', line 34

def field_types
  @result.fields.map{|f| field_type_string(f)}
end

#fieldsObject



30
31
32
# File 'lib/mysql2/result.rb', line 30

def fields
  @result.fields.map{|f| f.name.freeze}
end

#freeObject



26
27
28
# File 'lib/mysql2/result.rb', line 26

def free
  # dummy
end

#sizeObject Also known as: count



21
22
23
# File 'lib/mysql2/result.rb', line 21

def size
  @result.size
end