Class | Sequel::MySQL::Database |
In: |
lib/sequel/adapters/mysql.rb
|
Parent: | Sequel::Database |
Connect to the database. In addition to the usual database options, the following options have effect:
# 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
Returns instance of Sequel::MySQL::Dataset with the given options.
# File lib/sequel/adapters/mysql.rb, line 115 115: def dataset(opts = nil) 116: MySQL::Dataset.new(self, opts) 117: end
Executes the given SQL using an available connection, yielding the connection if the block is given.
# 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
Support single level transactions on MySQL.
# 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