Class: Mysql::Result

Inherits:
Object show all
Defined in:
lib/sequel_core/adapters/mysql.rb

Overview

Add methods to get columns, yield hashes with symbol keys, and do type conversion.

Constant Summary collapse

MYSQL_TYPES =

Mapping of type numbers to conversion methods.

{
  0   => :to_d,     # MYSQL_TYPE_DECIMAL
  1   => :to_i,     # MYSQL_TYPE_TINY
  2   => :to_i,     # MYSQL_TYPE_SHORT
  3   => :to_i,     # MYSQL_TYPE_LONG
  4   => :to_f,     # MYSQL_TYPE_FLOAT
  5   => :to_f,     # MYSQL_TYPE_DOUBLE
  # 6   => ??,        # MYSQL_TYPE_NULL
  7   => :to_sequel_time,  # MYSQL_TYPE_TIMESTAMP
  8   => :to_i,     # MYSQL_TYPE_LONGLONG
  9   => :to_i,     # MYSQL_TYPE_INT24
  10  => :to_date,  # MYSQL_TYPE_DATE
  11  => :to_time,  # MYSQL_TYPE_TIME
  12  => :to_sequel_time,  # MYSQL_TYPE_DATETIME
  13  => :to_i,     # MYSQL_TYPE_YEAR
  14  => :to_date,  # MYSQL_TYPE_NEWDATE
  # 15  => :to_s      # MYSQL_TYPE_VARCHAR
  # 16  => :to_s,     # MYSQL_TYPE_BIT
  246 => :to_d,     # MYSQL_TYPE_NEWDECIMAL
  247 => :to_i,     # MYSQL_TYPE_ENUM
  248 => :to_i,      # MYSQL_TYPE_SET
  249 => :to_blob,     # MYSQL_TYPE_TINY_BLOB
  250 => :to_blob,     # MYSQL_TYPE_MEDIUM_BLOB
  251 => :to_blob,     # MYSQL_TYPE_LONG_BLOB
  252 => :to_blob,     # MYSQL_TYPE_BLOB
  # 253 => :to_s,     # MYSQL_TYPE_VAR_STRING
  # 254 => :to_s,     # MYSQL_TYPE_STRING
  # 255 => :to_s      # MYSQL_TYPE_GEOMETRY
}

Instance Method Summary collapse

Instance Method Details

#columns(with_table = nil) ⇒ Object

Return an array of column name symbols for this result set.



39
40
41
42
43
44
45
46
47
48
# File 'lib/sequel_core/adapters/mysql.rb', line 39

def columns(with_table = nil)
  unless @columns
    @column_types = []
    @columns = fetch_fields.map do |f|
      @column_types << f.type
      (with_table ? "#{f.table}.#{f.name}" : f.name).to_sym
    end
  end
  @columns
end

#sequel_each_hash(with_table = nil) ⇒ Object

yield a hash with symbol keys and type converted values.



51
52
53
54
55
56
57
58
# File 'lib/sequel_core/adapters/mysql.rb', line 51

def sequel_each_hash(with_table = nil)
  c = columns
  while row = fetch_row
    h = {}
    c.each_with_index {|f, i| h[f] = convert_type(row[i], @column_types[i])}
    yield h
  end
end