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

Database class for MySQL databases used with Sequel.

### Methods

connect dataset execute server_version

### Included Modules

Sequel::MySQL::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)/ |  | Mysql::Error messages that indicate the current connection should be disconnected

### Public Instance methods

connect(server)

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)
- :compress - Set to false to not compress results from the server
- :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.
- :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.

[Source]

        # File lib/sequel/adapters/mysql2.rb, line 34
    34:       def connect(server)
    35:         opts = server_opts(server)
    36:         opts[:host] ||= 'localhost'
    37:         opts[:username] ||= opts[:user]
    38:         conn = ::Mysql2::Client.new(opts)
    39:
    40:         sqls = []
    41:         # Set encoding a slightly different way after connecting,
    42:         # in case the READ_DEFAULT_GROUP overrode the provided encoding.
    43:         # Doesn't work across implicit reconnects, but Sequel doesn't turn on
    44:         # that feature.
    45:         if encoding = opts[:encoding] || opts[:charset]
    46:           sqls << "SET NAMES #{conn.escape(encoding.to_s)}"
    47:         end
    48:
    49:         # Increase timeout so mysql server doesn't disconnect us.
    50:         # Value used by default is maximum allowed value on Windows.
    51:         sqls << "SET @@wait_timeout = #{opts[:timeout] || 2147483}"
    52:
    53:         # By default, MySQL 'where id is null' selects the last inserted id
    54:         sqls << "SET SQL_AUTO_IS_NULL=0" unless opts[:auto_is_null]
    55:
    56:         sqls.each{|sql| log_yield(sql){conn.query(sql)}}
    57:
    58:         conn
    59:       end

dataset(opts = nil)

Returns instance of Sequel::MySQL::Dataset with the given options.

[Source]

        # File lib/sequel/adapters/mysql2.rb, line 62
    62:       def dataset(opts = nil)
    63:         Mysql2::Dataset.new(self, opts)
    64:       end

execute(sql, opts={}, &block)

Executes the given SQL using an available connection, yielding the connection if the block is given.

[Source]

        # File lib/sequel/adapters/mysql2.rb, line 68
    68:       def execute(sql, opts={}, &block)
    69:         if opts[:sproc]
    70:           call_sproc(sql, opts, &block)
    71:         else
    72:           synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
    73:         end
    74:       end

server_version(server=nil)

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

[Source]

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

[Validate]