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 transaction

### Included Modules

Sequel::MySQL::DatabaseMethods

### Public Instance methods

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

Support stored procedures on MySQL

[Source]

        # File lib/sequel/adapters/mysql.rb, line 61
    61:       def call_sproc(name, opts={}, &block)
    62:         args = opts[:args] || []
    63:         execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block)
    64:       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 79
     79:       def connect(server)
     80:         opts = server_opts(server)
     81:         conn = Mysql.init
     82:         conn.options(Mysql::OPT_LOCAL_INFILE, "client")
     83:         if encoding = opts[:encoding] || opts[:charset]
     84:           # set charset _before_ the connect. using an option instead of "SET (NAMES|CHARACTER_SET_*)" works across reconnects
     85:           conn.options(Mysql::SET_CHARSET_NAME, encoding)
     86:         end
     87:         conn.real_connect(
     88:           opts[:host] || 'localhost',
     89:           opts[:user],
     90:           opts[:password],
     91:           opts[:database],
     92:           opts[:port],
     93:           opts[:socket],
     94:           Mysql::CLIENT_MULTI_RESULTS +
     95:           Mysql::CLIENT_MULTI_STATEMENTS +
     96:           (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
     97:         )
     98:
     99:         # increase timeout so mysql server doesn't disconnect us
    100:         conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}")
    101:
    102:         # By default, MySQL 'where id is null' selects the last inserted id
    103:         conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null]
    104:
    105:         conn.query_with_result = false
    106:         class << conn
    107:           attr_accessor :prepared_statements
    108:         end
    109:         conn.prepared_statements = {}
    110:         conn.reconnect = true
    111:         conn
    112:       end

dataset(opts = nil)

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

[Source]

         # File lib/sequel/adapters/mysql.rb, line 115
    115:       def dataset(opts = nil)
    116:         MySQL::Dataset.new(self, opts)
    117:       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 121
    121:       def execute(sql, opts={}, &block)
    122:         return call_sproc(sql, opts, &block) if opts[:sproc]
    123:         return execute_prepared_statement(sql, opts, &block) if Symbol === sql
    124:         begin
    125:           synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
    126:         rescue Mysql::Error => e
    127:           raise_error(e)
    128:         end
    129:       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 132
    132:       def server_version(server=nil)
    133:         @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
    134:       end

transaction(opts={}) {|conn| ...}

Support single level transactions on MySQL.

[Source]

         # File lib/sequel/adapters/mysql.rb, line 137
    137:       def transaction(opts={})
    138:         synchronize(opts[:server]) do |conn|
    139:           return yield(conn) if @transactions.include?(Thread.current)
    140:           log_info(begin_transaction_sql)
    141:           conn.query(begin_transaction_sql)
    142:           begin
    143:             @transactions << Thread.current
    144:             yield(conn)
    145:           rescue ::Exception => e
    146:             log_info(rollback_transaction_sql)
    147:             conn.query(rollback_transaction_sql)
    148:             transaction_error(e, Mysql::Error)
    149:           ensure
    150:             unless e
    151:               log_info(commit_transaction_sql)
    152:               conn.query(commit_transaction_sql)
    153:             end
    154:             @transactions.delete(Thread.current)
    155:           end
    156:         end
    157:       end

[Validate]