Class | Sequel::MySQL::Database |
In: |
lib/sequel/adapters/mysql.rb
|
Parent: | Sequel::Database |
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 |
Connect to the database. In addition to the usual database options, the following options have effect:
# File lib/sequel/adapters/mysql.rb, line 97 97: def connect(server) 98: opts = server_opts(server) 99: conn = Mysql.init 100: conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client") 101: conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile) 102: if encoding = opts[:encoding] || opts[:charset] 103: # Set encoding before connecting so that the mysql driver knows what 104: # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP. 105: conn.options(Mysql::SET_CHARSET_NAME, encoding) 106: end 107: conn.real_connect( 108: opts[:host] || 'localhost', 109: opts[:user], 110: opts[:password], 111: opts[:database], 112: opts[:port], 113: opts[:socket], 114: Mysql::CLIENT_MULTI_RESULTS + 115: Mysql::CLIENT_MULTI_STATEMENTS + 116: (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) 117: ) 118: # Set encoding a slightly different way after connecting, 119: # in case the READ_DEFAULT_GROUP overrode the provided encoding. 120: # Doesn't work across implicit reconnects, but Sequel doesn't turn on 121: # that feature. 122: conn.query("set names #{literal(encoding.to_s)}") if encoding 123: 124: # increase timeout so mysql server doesn't disconnect us 125: conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}") 126: 127: # By default, MySQL 'where id is null' selects the last inserted id 128: conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null] 129: 130: class << conn 131: attr_accessor :prepared_statements 132: end 133: conn.prepared_statements = {} 134: conn 135: end
Returns instance of Sequel::MySQL::Dataset with the given options.
# File lib/sequel/adapters/mysql.rb, line 138 138: def dataset(opts = nil) 139: MySQL::Dataset.new(self, opts) 140: end
Executes the given SQL using an available connection, yielding the connection if the block is given.
# File lib/sequel/adapters/mysql.rb, line 144 144: def execute(sql, opts={}, &block) 145: if opts[:sproc] 146: call_sproc(sql, opts, &block) 147: elsif sql.is_a?(Symbol) 148: execute_prepared_statement(sql, opts, &block) 149: else 150: synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)} 151: end 152: end