Class: Sequel::MySQL::Database

Class | Sequel::MySQL::Database
In: | lib/sequel_core/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_core/adapters/mysql.rb, line 46
    46:       def call_sproc(name, opts={}, &block)
    47:         args = opts[:args] || []
    48:         execute("CALL #{name}(#{literal(args) unless args.empty?})", opts.merge(:sproc=>false), &block)
    49:       end

connect(server)

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

- :encoding, :charset - 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.

[Source]

        # File lib/sequel_core/adapters/mysql.rb, line 57
    57:       def connect(server)
    58:         opts = server_opts(server)
    59:         conn = Mysql.init
    60:         conn.options(Mysql::OPT_LOCAL_INFILE, "client")
    61:         conn.real_connect(
    62:           opts[:host] || 'localhost',
    63:           opts[:user],
    64:           opts[:password],
    65:           opts[:database],
    66:           opts[:port],
    67:           opts[:socket],
    68:           Mysql::CLIENT_MULTI_RESULTS +
    69:           Mysql::CLIENT_MULTI_STATEMENTS +
    70:           Mysql::CLIENT_COMPRESS
    71:         )
    72:         conn.query_with_result = false
    73:         if encoding = opts[:encoding] || opts[:charset]
    74:           conn.query("set character_set_connection = '#{encoding}'")
    75:           conn.query("set character_set_client = '#{encoding}'")
    76:           conn.query("set character_set_database = '#{encoding}'")
    77:           conn.query("set character_set_server = '#{encoding}'")
    78:           conn.query("set character_set_results = '#{encoding}'")
    79:         end
    80:         conn.meta_eval{attr_accessor :prepared_statements}
    81:         conn.prepared_statements = {}
    82:         conn.reconnect = true
    83:         conn
    84:       end

dataset(opts = nil)

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

[Source]

        # File lib/sequel_core/adapters/mysql.rb, line 87
    87:       def dataset(opts = nil)
    88:         MySQL::Dataset.new(self, opts)
    89:       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_core/adapters/mysql.rb, line 93
     93:       def execute(sql, opts={}, &block)
     94:         return call_sproc(sql, opts, &block) if opts[:sproc]
     95:         return execute_prepared_statement(sql, opts, &block) if Symbol === sql
     96:         begin
     97:           synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
     98:         rescue Mysql::Error => e
     99:           raise_error(e)
    100:         end
    101:       end

server_version(server=nil)

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

[Source]

         # File lib/sequel_core/adapters/mysql.rb, line 104
    104:       def server_version(server=nil)
    105:         @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
    106:       end

transaction(server=nil) {|conn| ...}

Support single level transactions on MySQL.

[Source]

         # File lib/sequel_core/adapters/mysql.rb, line 109
    109:       def transaction(server=nil)
    110:         synchronize(server) do |conn|
    111:           return yield(conn) if @transactions.include?(Thread.current)
    112:           log_info(begin_transaction_sql)
    113:           conn.query(begin_transaction_sql)
    114:           begin
    115:             @transactions << Thread.current
    116:             yield(conn)
    117:           rescue ::Exception => e
    118:             log_info(rollback_transaction_sql)
    119:             conn.query(rollback_transaction_sql)
    120:             transaction_error(e, Mysql::Error)
    121:           ensure
    122:             unless e
    123:               log_info(commit_transaction_sql)
    124:               conn.query(commit_transaction_sql)
    125:             end
    126:             @transactions.delete(Thread.current)
    127:           end
    128:         end
    129:       end

[Validate]