Module: Sequel::MySQL

Defined in:
lib/sequel/lib/sequel/adapters/mysql.rb,
lib/sequel/lib/sequel/adapters/shared/mysql.rb

Overview

Module for holding all MySQL-related classes and modules for Sequel.

A class level convert_invalid_date_time accessor exists if the native adapter is used. If set to nil or :nil, the adapter treats dates like 0000-00-00 and times like 838:00:00 as nil values. If set to :string, it returns the strings as is. It is false by default, which means that invalid dates and times will raise errors.

Sequel::MySQL.convert_invalid_date_time = true

Sequel converts the column type tinyint(1) to a boolean by default when using the native MySQL adapter. You can turn off the conversion to use tinyint as an integer:

Sequel.convert_tinyint_to_bool = false

Defined Under Namespace

Modules: DatabaseMethods, DatasetMethods Classes: Database, Dataset

Constant Summary collapse

MYSQL_TYPES =

Mapping of type numbers to conversion procs

{}
MYSQL_TYPE_PROCS =

Use only a single proc for each type to save on memory

{
  [0, 246]  => lambda{|v| BigDecimal.new(v)},                         # decimal
  [1]  => lambda{|v| convert_tinyint_to_bool ? v.to_i != 0 : v.to_i}, # tinyint
  [2, 3, 8, 9, 13, 247, 248]  => lambda{|v| v.to_i},                  # integer
  [4, 5]  => lambda{|v| v.to_f},                                      # float
  [10, 14]  => lambda{|v| convert_date_time(:string_to_date, v)},     # date
  [7, 12] => lambda{|v| convert_date_time(:database_to_application_timestamp, v)},   # datetime
  [11]  => lambda{|v| convert_date_time(:string_to_time, v)},         # time
  [249, 250, 251, 252]  => lambda{|v| Sequel::SQL::Blob.new(v)}       # blob
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.convert_invalid_date_timeObject

Returns the value of attribute convert_invalid_date_time


44
45
46
# File 'lib/sequel/lib/sequel/adapters/mysql.rb', line 44

def convert_invalid_date_time
  @convert_invalid_date_time
end

.convert_tinyint_to_boolObject

Returns the value of attribute convert_tinyint_to_bool


44
45
46
# File 'lib/sequel/lib/sequel/adapters/mysql.rb', line 44

def convert_tinyint_to_bool
  @convert_tinyint_to_bool
end

.default_charsetObject

Set the default options used for CREATE TABLE


5
6
7
# File 'lib/sequel/lib/sequel/adapters/shared/mysql.rb', line 5

def default_charset
  @default_charset
end

.default_collateObject

Set the default options used for CREATE TABLE


5
6
7
# File 'lib/sequel/lib/sequel/adapters/shared/mysql.rb', line 5

def default_collate
  @default_collate
end

.default_engineObject

Set the default options used for CREATE TABLE


5
6
7
# File 'lib/sequel/lib/sequel/adapters/shared/mysql.rb', line 5

def default_engine
  @default_engine
end

Class Method Details

.convert_date_time(meth, v) ⇒ Object

If convert_invalid_date_time is nil, :nil, or :string and the conversion raises an InvalidValue exception, return v if :string and nil otherwise.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sequel/lib/sequel/adapters/mysql.rb', line 50

def self.convert_date_time(meth, v)
  begin
    Sequel.send(meth, v)
  rescue InvalidValue
    case @convert_invalid_date_time
    when nil, :nil
      nil
    when :string
      v
    else 
      raise
    end
  end
end