class Sequel::Mysql2::Database

Database class for MySQL databases used with Sequel.

Constants

DatasetClass

Attributes

convert_tinyint_to_bool[RW]

Whether to convert tinyint columns to bool for this database

Public Instance Methods

connect(server) click to toggle source

Connect to the database. In addition to the usual database options, the following options have effect:

  • :auto_is_null - Set to true to use MySQL default behavior of having a filter for an autoincrement column equals NULL to return the last inserted row.

  • :charset - Same as :encoding (:encoding takes precendence)

  • :encoding - Set all the related character sets for this connection (connection, client, database, server, and results).

The options hash is also passed to mysql2, and can include mysql2 options such as :local_infile.

# File lib/sequel/adapters/mysql2.rb, line 29
def connect(server)
  opts = server_opts(server)
  opts[:host] ||= 'localhost'
  opts[:username] ||= opts.delete(:user)
  opts[:flags] = ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
  conn = ::Mysql2::Client.new(opts)
  conn.query_options.merge!(:symbolize_keys=>true, :cache_rows=>false)

  sqls = mysql_connection_setting_sqls

  # Set encoding a slightly different way after connecting,
  # in case the READ_DEFAULT_GROUP overrode the provided encoding.
  # Doesn't work across implicit reconnects, but Sequel doesn't turn on
  # that feature.
  if encoding = opts[:encoding] || opts[:charset]
    sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}")
  end

  sqls.each{|sql| log_yield(sql){conn.query(sql)}}

  add_prepared_statements_cache(conn)
  conn
end
execute_dui(sql, opts={}) click to toggle source

Return the number of matched rows when executing a delete/update statement.

# File lib/sequel/adapters/mysql2.rb, line 54
def execute_dui(sql, opts={})
  execute(sql, opts){|c| return c.affected_rows}
end
execute_insert(sql, opts={}) click to toggle source

Return the last inserted id when executing an insert statement.

# File lib/sequel/adapters/mysql2.rb, line 59
def execute_insert(sql, opts={})
  execute(sql, opts){|c| return c.last_id}
end
server_version(server=nil) click to toggle source

Return the version of the MySQL server two which we are connecting.

# File lib/sequel/adapters/mysql2.rb, line 64
def server_version(server=nil)
  @server_version ||= (synchronize(server){|conn| conn.server_info[:id]} || super)
end

Private Instance Methods

_execute(conn, sql, opts) { |r| ... } click to toggle source

Execute the given SQL on the given connection. If the :type option is :select, yield the result of the query, otherwise yield the connection if a block is given.

# File lib/sequel/adapters/mysql2.rb, line 73
def _execute(conn, sql, opts)
  begin
    r = log_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql){conn.query(sql, :database_timezone => timezone, :application_timezone => Sequel.application_timezone)}
    if opts[:type] == :select
      yield r if r
    elsif block_given?
      yield conn
    end
  rescue ::Mysql2::Error => e
    raise_error(e)
  end
end
adapter_initialize() click to toggle source

Set the #convert_tinyint_to_bool setting based on the default value.

# File lib/sequel/adapters/mysql2.rb, line 87
def adapter_initialize
  self.convert_tinyint_to_bool = Sequel::MySQL.convert_tinyint_to_bool
end
connection_execute_method() click to toggle source

MySQL connections use the query method to execute SQL without a result

# File lib/sequel/adapters/mysql2.rb, line 92
def connection_execute_method
  :query
end
database_error_classes() click to toggle source

The MySQL adapter main error class is Mysql2::Error

# File lib/sequel/adapters/mysql2.rb, line 97
def database_error_classes
  [::Mysql2::Error]
end
database_exception_sqlstate(exception, opts) click to toggle source
# File lib/sequel/adapters/mysql2.rb, line 101
def database_exception_sqlstate(exception, opts)
  exception.sql_state
end
database_name() click to toggle source

The database name when using the native adapter is always stored in the :database option.

# File lib/sequel/adapters/mysql2.rb, line 118
def database_name
  @opts[:database]
end
disconnect_error?(e, opts) click to toggle source

If a connection object is available, try pinging it. Otherwise, if the error is a Mysql2::Error, check the SQL state and exception message for disconnects.

Calls superclass method Sequel::Database#disconnect_error?
# File lib/sequel/adapters/mysql2.rb, line 108
def disconnect_error?(e, opts)
  super ||
    ((conn = opts[:conn]) && !conn.ping) ||
    (e.is_a?(::Mysql2::Error) &&
      (e.sql_state =~ /\A08/ ||
       MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)))
end
schema_column_type(db_type) click to toggle source

Convert tinyint(1) type to boolean if #convert_tinyint_to_bool is true

# File lib/sequel/adapters/mysql2.rb, line 123
def schema_column_type(db_type)
  convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super
end