Class Sequel::Mysql2::Database
In: lib/sequel/adapters/mysql2.rb
Parent: Sequel::Database

Database class for MySQL databases used with Sequel.

Methods

Included Modules

Sequel::MySQL::DatabaseMethods Sequel::MySQL::PreparedStatements::DatabaseMethods

Constants

MYSQL_DATABASE_DISCONNECT_ERRORS = /\A(Commands out of sync; you can't run this command now|Can't connect to local MySQL server through socket|MySQL server has gone away|This connection is still waiting for a result, try again once you have the result|closed MySQL connection)/   Mysql::Error messages that indicate the current connection should be disconnected

Attributes

convert_tinyint_to_bool  [RW]  Whether to convert tinyint columns to bool for this database

Public Class methods

Set the convert_tinyint_to_bool setting based on the default value.

[Source]

    # File lib/sequel/adapters/mysql2.rb, line 21
21:       def initialize(opts={})
22:         super
23:         self.convert_tinyint_to_bool = Sequel::MySQL.convert_tinyint_to_bool
24:       end

Public Instance methods

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)
  • :config_default_group - The default group to read from the in the MySQL config file.
  • :config_local_infile - If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.
  • :connect_timeout - Set the timeout in seconds before a connection attempt is abandoned.
  • :encoding - Set all the related character sets for this connection (connection, client, database, server, and results).
  • :socket - Use a unix socket file instead of connecting via TCP/IP.
  • :timeout - Set the timeout in seconds before the server will disconnect this connection (a.k.a. @@wait_timeout).

[Source]

    # File lib/sequel/adapters/mysql2.rb, line 44
44:       def connect(server)
45:         opts = server_opts(server)
46:         opts[:host] ||= 'localhost'
47:         opts[:username] ||= opts[:user]
48:         opts[:flags] = ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS)
49:         conn = ::Mysql2::Client.new(opts)
50:         conn.query_options.merge!(:symbolize_keys=>true, :cache_rows=>false)
51: 
52:         sqls = mysql_connection_setting_sqls
53: 
54:         # Set encoding a slightly different way after connecting,
55:         # in case the READ_DEFAULT_GROUP overrode the provided encoding.
56:         # Doesn't work across implicit reconnects, but Sequel doesn't turn on
57:         # that feature.
58:         if encoding = opts[:encoding] || opts[:charset]
59:           sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}")
60:         end
61: 
62:         sqls.each{|sql| log_yield(sql){conn.query(sql)}}
63: 
64:         add_prepared_statements_cache(conn)
65:         conn
66:       end

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

[Source]

    # File lib/sequel/adapters/mysql2.rb, line 69
69:       def execute_dui(sql, opts={})
70:         execute(sql, opts){|c| return c.affected_rows}
71:       end

Return the last inserted id when executing an insert statement.

[Source]

    # File lib/sequel/adapters/mysql2.rb, line 74
74:       def execute_insert(sql, opts={})
75:         execute(sql, opts){|c| return c.last_id}
76:       end

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

[Source]

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

[Validate]