class Sequel::Mysql2::Database
Attributes
Whether to convert tinyint columns to bool for this database
Public Instance Methods
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)
- :encoding
-
Set all the related character sets for this connection (connection, client, database, server, and results).
The options hash is also passed to mysql2, and can include mysql2 options such as :local_infile.
# File lib/sequel/adapters/mysql2.rb 37 def connect(server) 38 opts = server_opts(server) 39 opts[:host] ||= 'localhost' 40 opts[:username] ||= opts.delete(:user) 41 opts[:flags] ||= 0 42 opts[:flags] |= ::Mysql2::Client::FOUND_ROWS if ::Mysql2::Client.const_defined?(:FOUND_ROWS) 43 opts[:encoding] ||= opts[:charset] 44 conn = ::Mysql2::Client.new(opts) 45 conn.query_options.merge!(:symbolize_keys=>true, :cache_rows=>false) 46 47 if NativePreparedStatements 48 conn.instance_variable_set(:@sequel_default_query_options, conn.query_options.dup) 49 end 50 51 sqls = mysql_connection_setting_sqls 52 53 # Set encoding a slightly different way after connecting, 54 # in case the READ_DEFAULT_GROUP overrode the provided encoding. 55 # Doesn't work across implicit reconnects, but Sequel doesn't turn on 56 # that feature. 57 if encoding = opts[:encoding] 58 sqls.unshift("SET NAMES #{conn.escape(encoding.to_s)}") 59 end 60 61 sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}} 62 63 add_prepared_statements_cache(conn) 64 conn 65 end
# File lib/sequel/adapters/mysql2.rb 67 def execute_dui(sql, opts=OPTS) 68 execute(sql, opts){|c| return c.affected_rows} 69 end
# File lib/sequel/adapters/mysql2.rb 71 def execute_insert(sql, opts=OPTS) 72 execute(sql, opts){|c| return c.last_id} 73 end
Sequel::MySQL::DatabaseMethods#freeze
# File lib/sequel/adapters/mysql2.rb 75 def freeze 76 server_version 77 super 78 end
Return the version of the MySQL
server to which we are connecting.
Sequel::MySQL::DatabaseMethods#server_version
# File lib/sequel/adapters/mysql2.rb 81 def server_version(server=nil) 82 @server_version ||= (synchronize(server){|conn| conn.server_info[:id]} || super) 83 end
Private Instance Methods
Execute the given SQL
on the given connection. If the :type option is :select, yield the result of the query, otherwise yield the connection if a block is given.
# File lib/sequel/adapters/mysql2.rb 114 def _execute(conn, sql, opts) 115 begin 116 stream = opts[:stream] 117 if NativePreparedStatements 118 if args = opts[:arguments] 119 args = args.map{|arg| bound_variable_value(arg)} 120 end 121 122 case sql 123 when ::Mysql2::Statement 124 stmt = sql 125 when Dataset 126 sql = sql.sql 127 close_stmt = true 128 stmt = conn.prepare(sql) 129 end 130 end 131 132 r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn, args) do 133 if stmt 134 conn.query_options.merge!(:cache_rows=>true, :database_timezone => timezone, :application_timezone => Sequel.application_timezone, :stream=>stream, :cast_booleans=>convert_tinyint_to_bool) 135 stmt.execute(*args) 136 else 137 conn.query(sql, :database_timezone => timezone, :application_timezone => Sequel.application_timezone, :stream=>stream) 138 end 139 end 140 if opts[:type] == :select 141 if r 142 if stream 143 begin 144 r2 = yield r 145 ensure 146 # If r2 is nil, it means the block did not exit normally, 147 # so the rest of the results must be drained to prevent 148 # "commands out of sync" errors. 149 r.each{} unless r2 150 end 151 else 152 yield r 153 end 154 end 155 elsif block_given? 156 yield conn 157 end 158 rescue ::Mysql2::Error => e 159 raise_error(e) 160 ensure 161 if stmt 162 conn.query_options.replace(conn.instance_variable_get(:@sequel_default_query_options)) 163 stmt.close if close_stmt 164 end 165 end 166 end
Set the convert_tinyint_to_bool
setting based on the default value.
# File lib/sequel/adapters/mysql2.rb 169 def adapter_initialize 170 self.convert_tinyint_to_bool = true 171 end
Handle bound variable arguments that Mysql2
does not handle natively.
# File lib/sequel/adapters/mysql2.rb 175 def bound_variable_value(arg) 176 case arg 177 when true 178 1 179 when false 180 0 181 when DateTime, Time 182 literal(arg)[1...-1] 183 else 184 arg 185 end 186 end
# File lib/sequel/adapters/mysql2.rb 189 def connection_execute_method 190 :query 191 end
# File lib/sequel/adapters/mysql2.rb 193 def database_error_classes 194 [::Mysql2::Error] 195 end
# File lib/sequel/adapters/mysql2.rb 197 def database_exception_sqlstate(exception, opts) 198 state = exception.sql_state 199 state unless state == 'HY000' 200 end
# File lib/sequel/adapters/mysql2.rb 202 def dataset_class_default 203 Dataset 204 end
If a connection object is available, try pinging it. Otherwise, if the error is a Mysql2::Error, check the SQL
state and exception message for disconnects.
Sequel::Database#disconnect_error?
# File lib/sequel/adapters/mysql2.rb 209 def disconnect_error?(e, opts) 210 super || 211 ((conn = opts[:conn]) && !conn.ping) || 212 (e.is_a?(::Mysql2::Error) && 213 (e.sql_state =~ /\A08/ || 214 MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message))) 215 end
Use a native mysql2 prepared statement to implement prepared statements.
# File lib/sequel/adapters/mysql2.rb 89 def execute_prepared_statement(ps_name, opts, &block) 90 ps = prepared_statement(ps_name) 91 sql = ps.prepared_sql 92 93 synchronize(opts[:server]) do |conn| 94 stmt, ps_sql = conn.prepared_statements[ps_name] 95 unless ps_sql == sql 96 stmt.close if stmt 97 stmt = log_connection_yield(conn, "Preparing #{ps_name}: #{sql}"){conn.prepare(sql)} 98 conn.prepared_statements[ps_name] = [stmt, sql] 99 end 100 101 if ps.log_sql 102 opts = Hash[opts] 103 opts = opts[:log_sql] = " (#{sql})" 104 end 105 106 _execute(conn, stmt, opts, &block) 107 end 108 end
Convert tinyint(1) type to boolean if convert_tinyint_to_bool
is true
Sequel::MySQL::DatabaseMethods#schema_column_type
# File lib/sequel/adapters/mysql2.rb 218 def schema_column_type(db_type) 219 convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super 220 end