class Sequel::MySQL::Database
Attributes
Hash
of conversion procs for the current database
By default, Sequel
raises an exception if in invalid date or time is used. However, if this is set to nil or :nil, the adapter treats dates like 0000-00-00 and times like 838:00:00 as nil values. If set to :string, it returns the strings as is.
Whether to convert tinyint columns to bool for the current 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)
- :compress
-
Set to false to not compress results from the server
- :config_default_group
-
The default group to read from the in the
MySQL
config file. - :config_local_infile
-
If provided, sets the Mysql::OPT_LOCAL_INFILE option on the connection with the given value.
- :connect_timeout
-
Set the timeout in seconds before a connection attempt is abandoned.
- :encoding
-
Set all the related character sets for this connection (connection, client, database, server, and results).
- :read_timeout
-
Set the timeout in seconds for reading back results to a query.
- :socket
-
Use a unix socket file instead of connecting via TCP/IP.
- :timeout
-
Set the timeout in seconds before the server will disconnect this connection (a.k.a @@wait_timeout).
# File lib/sequel/adapters/mysql.rb 72 def connect(server) 73 opts = server_opts(server) 74 75 if Mysql.respond_to?(:init) 76 conn = Mysql.init 77 conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client") 78 conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile) 79 if encoding = opts[:encoding] || opts[:charset] 80 # Set encoding before connecting so that the mysql driver knows what 81 # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP. 82 conn.options(Mysql::SET_CHARSET_NAME, encoding) 83 end 84 if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT 85 conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout) 86 end 87 if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT 88 conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout) 89 end 90 else 91 # ruby-mysql 3 API 92 conn = Mysql.new 93 # no support for default group 94 conn.local_infile = opts[:config_local_infile] if opts.has_key?(:config_local_infile) 95 if encoding = opts[:encoding] || opts[:charset] 96 conn.charset = encoding 97 end 98 if read_timeout = opts[:read_timeout] 99 conn.read_timeout = read_timeout 100 end 101 if connect_timeout = opts[:connect_timeout] 102 conn.connect_timeout = connect_timeout 103 end 104 conn.singleton_class.class_eval do 105 alias real_connect connect 106 alias use_result store_result 107 end 108 end 109 110 conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey] 111 conn.real_connect( 112 opts[:host] || 'localhost', 113 opts[:user], 114 opts[:password], 115 opts[:database], 116 (opts[:port].to_i if opts[:port]), 117 opts[:socket], 118 Mysql::CLIENT_MULTI_RESULTS + 119 Mysql::CLIENT_MULTI_STATEMENTS + 120 (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) 121 ) 122 sqls = mysql_connection_setting_sqls 123 124 # Set encoding a slightly different way after connecting, 125 # in case the READ_DEFAULT_GROUP overrode the provided encoding. 126 # Doesn't work across implicit reconnects, but Sequel doesn't turn on 127 # that feature. 128 sqls.unshift("SET NAMES #{literal(encoding.to_s)}") if encoding 129 130 sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}} 131 132 add_prepared_statements_cache(conn) 133 conn 134 end
Modify the type translators for the date, time, and timestamp types depending on the value given.
# File lib/sequel/adapters/mysql.rb 144 def convert_invalid_date_time=(v) 145 m0 = ::Sequel.method(:string_to_time) 146 @conversion_procs[11] = (v != false) ? lambda{|val| convert_date_time(val, &m0)} : m0 147 m1 = ::Sequel.method(:string_to_date) 148 m = (v != false) ? lambda{|val| convert_date_time(val, &m1)} : m1 149 [10, 14].each{|i| @conversion_procs[i] = m} 150 m2 = method(:to_application_timestamp) 151 m = (v != false) ? lambda{|val| convert_date_time(val, &m2)} : m2 152 [7, 12].each{|i| @conversion_procs[i] = m} 153 @convert_invalid_date_time = v 154 end
Modify the type translator used for the tinyint type based on the value given.
# File lib/sequel/adapters/mysql.rb 158 def convert_tinyint_to_bool=(v) 159 @conversion_procs[1] = v ? TYPE_TRANSLATOR_BOOLEAN : TYPE_TRANSLATOR_INTEGER 160 @convert_tinyint_to_bool = v 161 end
# File lib/sequel/adapters/mysql.rb 136 def disconnect_connection(c) 137 c.close 138 rescue Mysql::Error 139 nil 140 end
# File lib/sequel/adapters/mysql.rb 163 def execute_dui(sql, opts=OPTS) 164 execute(sql, opts){|c| return affected_rows(c)} 165 end
# File lib/sequel/adapters/mysql.rb 167 def execute_insert(sql, opts=OPTS) 168 execute(sql, opts){|c| return c.insert_id} 169 end
Sequel::MySQL::DatabaseMethods#freeze
# File lib/sequel/adapters/mysql.rb 171 def freeze 172 server_version 173 @conversion_procs.freeze 174 super 175 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/mysql.rb 182 def _execute(conn, sql, opts) 183 r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)} 184 if opts[:type] == :select 185 yield r if r 186 elsif defined?(yield) 187 yield conn 188 end 189 if conn.respond_to?(:more_results?) 190 while conn.more_results? do 191 if r 192 r.free 193 r = nil 194 end 195 begin 196 conn.next_result 197 r = conn.use_result 198 rescue Mysql::Error => e 199 raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message) 200 break 201 end 202 yield r if opts[:type] == :select 203 end 204 end 205 rescue Mysql::Error => e 206 raise_error(e) 207 ensure 208 r.free if r 209 # Use up all results to avoid a commands out of sync message. 210 if conn.respond_to?(:more_results?) 211 while conn.more_results? do 212 begin 213 conn.next_result 214 r = conn.use_result 215 rescue Mysql::Error => e 216 raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message) 217 break 218 end 219 r.free if r 220 end 221 end 222 end
# File lib/sequel/adapters/mysql.rb 224 def adapter_initialize 225 @conversion_procs = MYSQL_TYPES.dup 226 self.convert_tinyint_to_bool = true 227 self.convert_invalid_date_time = false 228 end
Try to get an accurate number of rows matched using the query info. Fall back to affected_rows
if there was no match, but that may be inaccurate.
# File lib/sequel/adapters/mysql.rb 233 def affected_rows(conn) 234 s = conn.info 235 if s && s =~ /Rows matched:\s+(\d+)\s+Changed:\s+\d+\s+Warnings:\s+\d+/ 236 $1.to_i 237 else 238 conn.affected_rows 239 end 240 end
If convert_invalid_date_time
is nil, :nil, or :string and the conversion raises an InvalidValue exception, return v if :string and nil otherwise.
# File lib/sequel/adapters/mysql.rb 250 def convert_date_time(v) 251 yield v 252 rescue InvalidValue 253 case @convert_invalid_date_time 254 when nil, :nil 255 nil 256 when :string 257 v 258 else 259 raise 260 end 261 end
# File lib/sequel/adapters/mysql.rb 263 def database_error_classes 264 [Mysql::Error] 265 end
# File lib/sequel/adapters/mysql.rb 267 def database_exception_sqlstate(exception, opts) 268 exception.sqlstate 269 end
# File lib/sequel/adapters/mysql.rb 271 def dataset_class_default 272 Dataset 273 end
Sequel::Database#disconnect_error?
# File lib/sequel/adapters/mysql.rb 275 def disconnect_error?(e, opts) 276 super || (e.is_a?(::Mysql::Error) && MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)) 277 end
Convert tinyint(1) type to boolean if convert_tinyint_to_bool
is true
Sequel::MySQL::DatabaseMethods#schema_column_type
# File lib/sequel/adapters/mysql.rb 280 def schema_column_type(db_type) 281 convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super 282 end