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 91 91: def connect(server) 92: opts = server_opts(server) 93: conn = Mysql.init 94: conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client") 95: conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile) 96: if encoding = opts[:encoding] || opts[:charset] 97: # Set encoding before connecting so that the mysql driver knows what 98: # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP. 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: sqls = [] 113: # Set encoding a slightly different way after connecting, 114: # in case the READ_DEFAULT_GROUP overrode the provided encoding. 115: # Doesn't work across implicit reconnects, but Sequel doesn't turn on 116: # that feature. 117: sqls << "SET NAMES #{literal(encoding.to_s)}" if encoding 118: 119: # increase timeout so mysql server doesn't disconnect us 120: sqls << "SET @@wait_timeout = #{opts[:timeout] || 2592000}" 121: 122: # By default, MySQL 'where id is null' selects the last inserted id 123: sqls << "SET SQL_AUTO_IS_NULL=0" unless opts[:auto_is_null] 124: 125: sqls.each{|sql| log_yield(sql){conn.query(sql)}} 126: 127: class << conn 128: attr_accessor :prepared_statements 129: end 130: conn.prepared_statements = {} 131: conn 132: end
Returns instance of Sequel::MySQL::Dataset with the given options.
# File lib/sequel/adapters/mysql.rb, line 135 135: def dataset(opts = nil) 136: MySQL::Dataset.new(self, opts) 137: end
Executes the given SQL using an available connection, yielding the connection if the block is given.
# File lib/sequel/adapters/mysql.rb, line 141 141: def execute(sql, opts={}, &block) 142: if opts[:sproc] 143: call_sproc(sql, opts, &block) 144: elsif sql.is_a?(Symbol) 145: execute_prepared_statement(sql, opts, &block) 146: else 147: synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)} 148: end 149: end