Class | Sequel::MySQL::Database
In: | lib/sequel/adapters/mysql.rb
Parent: | Sequel::Database

Database class for MySQL databases used with Sequel.

### Methods

call_sproc 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\z|Can't connect to local MySQL server through socket)/ |  | Mysql::Error messages that indicate the current connection should be disconnected

### Public Instance methods

call_sproc(name, opts={}, &block)

Support stored procedures on MySQL

[Source]

        # File lib/sequel/adapters/mysql.rb, line 75
    75:       def call_sproc(name, opts={}, &block)
    76:         args = opts[:args] || []
    77:         execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block)
    78:       end

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
- :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/mysql.rb, line 93
     93:       def connect(server)
     94:         opts = server_opts(server)
     95:         conn = Mysql.init
     96:         conn.options(Mysql::OPT_LOCAL_INFILE, "client")
     97:         if encoding = opts[:encoding] || opts[:charset]
     98:           # set charset _before_ the connect. using an option instead of "SET (NAMES|CHARACTER_SET_*)" works across reconnects
     99:           conn.options(Mysql::SET_CHARSET_NAME, encoding)
    100:         end
    101:         conn.real_connect(
    102:           opts[:host] || 'localhost',
    103:           opts[:user],
    104:           opts[:password],
    105:           opts[:database],
    106:           opts[:port],
    107:           opts[:socket],
    108:           Mysql::CLIENT_MULTI_RESULTS +
    109:           Mysql::CLIENT_MULTI_STATEMENTS +
    110:           (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
    111:         )
    112:
    113:         # increase timeout so mysql server doesn't disconnect us
    114:         conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}")
    115:
    116:         # By default, MySQL 'where id is null' selects the last inserted id
    117:         conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null]
    118:
    119:         class << conn
    120:           attr_accessor :prepared_statements
    121:         end
    122:         conn.prepared_statements = {}
    123:         conn.reconnect = true
    124:         conn
    125:       end

dataset(opts = nil)

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

[Source]

         # File lib/sequel/adapters/mysql.rb, line 128
    128:       def dataset(opts = nil)
    129:         MySQL::Dataset.new(self, opts)
    130:       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/mysql.rb, line 134
    134:       def execute(sql, opts={}, &block)
    135:         if opts[:sproc]
    136:           call_sproc(sql, opts, &block)
    137:         elsif sql.is_a?(Symbol)
    138:           execute_prepared_statement(sql, opts, &block)
    139:         else
    140:           synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
    141:         end
    142:       end

server_version(server=nil)

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

[Source]

         # File lib/sequel/adapters/mysql.rb, line 145
    145:       def server_version(server=nil)
    146:         @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
    147:       end

[Validate]