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 conn = Mysql.init 75 conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client") 76 conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile) 77 conn.ssl_set(opts[:sslkey], opts[:sslcert], opts[:sslca], opts[:sslcapath], opts[:sslcipher]) if opts[:sslca] || opts[:sslkey] 78 if encoding = opts[:encoding] || opts[:charset] 79 # Set encoding before connecting so that the mysql driver knows what 80 # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP. 81 conn.options(Mysql::SET_CHARSET_NAME, encoding) 82 end 83 if read_timeout = opts[:read_timeout] and defined? Mysql::OPT_READ_TIMEOUT 84 conn.options(Mysql::OPT_READ_TIMEOUT, read_timeout) 85 end 86 if connect_timeout = opts[:connect_timeout] and defined? Mysql::OPT_CONNECT_TIMEOUT 87 conn.options(Mysql::OPT_CONNECT_TIMEOUT, connect_timeout) 88 end 89 conn.real_connect( 90 opts[:host] || 'localhost', 91 opts[:user], 92 opts[:password], 93 opts[:database], 94 (opts[:port].to_i if opts[:port]), 95 opts[:socket], 96 Mysql::CLIENT_MULTI_RESULTS + 97 Mysql::CLIENT_MULTI_STATEMENTS + 98 (opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS) 99 ) 100 sqls = mysql_connection_setting_sqls 101 102 # Set encoding a slightly different way after connecting, 103 # in case the READ_DEFAULT_GROUP overrode the provided encoding. 104 # Doesn't work across implicit reconnects, but Sequel doesn't turn on 105 # that feature. 106 sqls.unshift("SET NAMES #{literal(encoding.to_s)}") if encoding 107 108 sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}} 109 110 add_prepared_statements_cache(conn) 111 conn 112 end
Modify the type translators for the date, time, and timestamp types depending on the value given.
# File lib/sequel/adapters/mysql.rb 122 def convert_invalid_date_time=(v) 123 m0 = ::Sequel.method(:string_to_time) 124 @conversion_procs[11] = (v != false) ? lambda{|val| convert_date_time(val, &m0)} : m0 125 m1 = ::Sequel.method(:string_to_date) 126 m = (v != false) ? lambda{|val| convert_date_time(val, &m1)} : m1 127 [10, 14].each{|i| @conversion_procs[i] = m} 128 m2 = method(:to_application_timestamp) 129 m = (v != false) ? lambda{|val| convert_date_time(val, &m2)} : m2 130 [7, 12].each{|i| @conversion_procs[i] = m} 131 @convert_invalid_date_time = v 132 end
Modify the type translator used for the tinyint type based on the value given.
# File lib/sequel/adapters/mysql.rb 136 def convert_tinyint_to_bool=(v) 137 @conversion_procs[1] = v ? TYPE_TRANSLATOR_BOOLEAN : TYPE_TRANSLATOR_INTEGER 138 @convert_tinyint_to_bool = v 139 end
# File lib/sequel/adapters/mysql.rb 114 def disconnect_connection(c) 115 c.close 116 rescue Mysql::Error 117 nil 118 end
# File lib/sequel/adapters/mysql.rb 141 def execute_dui(sql, opts=OPTS) 142 execute(sql, opts){|c| return affected_rows(c)} 143 end
# File lib/sequel/adapters/mysql.rb 145 def execute_insert(sql, opts=OPTS) 146 execute(sql, opts){|c| return c.insert_id} 147 end
Sequel::MySQL::DatabaseMethods#freeze
# File lib/sequel/adapters/mysql.rb 149 def freeze 150 server_version 151 @conversion_procs.freeze 152 super 153 end
Return the version of the MySQL
server to which we are connecting.
Sequel::MySQL::DatabaseMethods#server_version
# File lib/sequel/adapters/mysql.rb 156 def server_version(server=nil) 157 @server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super) 158 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 165 def _execute(conn, sql, opts) 166 begin 167 r = log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn){conn.query(sql)} 168 if opts[:type] == :select 169 yield r if r 170 elsif block_given? 171 yield conn 172 end 173 if conn.respond_to?(:more_results?) 174 while conn.more_results? do 175 if r 176 r.free 177 r = nil 178 end 179 begin 180 conn.next_result 181 r = conn.use_result 182 rescue Mysql::Error => e 183 raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message) 184 break 185 end 186 yield r if opts[:type] == :select 187 end 188 end 189 rescue Mysql::Error => e 190 raise_error(e) 191 ensure 192 r.free if r 193 # Use up all results to avoid a commands out of sync message. 194 if conn.respond_to?(:more_results?) 195 while conn.more_results? do 196 begin 197 conn.next_result 198 r = conn.use_result 199 rescue Mysql::Error => e 200 raise_error(e, :disconnect=>true) if MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message) 201 break 202 end 203 r.free if r 204 end 205 end 206 end 207 end
# File lib/sequel/adapters/mysql.rb 209 def adapter_initialize 210 @conversion_procs = MYSQL_TYPES.dup 211 self.convert_tinyint_to_bool = true 212 self.convert_invalid_date_time = false 213 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 218 def affected_rows(conn) 219 s = conn.info 220 if s && s =~ /Rows matched:\s+(\d+)\s+Changed:\s+\d+\s+Warnings:\s+\d+/ 221 $1.to_i 222 else 223 conn.affected_rows 224 end 225 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 235 def convert_date_time(v) 236 begin 237 yield v 238 rescue InvalidValue 239 case @convert_invalid_date_time 240 when nil, :nil 241 nil 242 when :string 243 v 244 else 245 raise 246 end 247 end 248 end
# File lib/sequel/adapters/mysql.rb 250 def database_error_classes 251 [Mysql::Error] 252 end
# File lib/sequel/adapters/mysql.rb 254 def database_exception_sqlstate(exception, opts) 255 exception.sqlstate 256 end
# File lib/sequel/adapters/mysql.rb 258 def dataset_class_default 259 Dataset 260 end
Sequel::Database#disconnect_error?
# File lib/sequel/adapters/mysql.rb 262 def disconnect_error?(e, opts) 263 super || (e.is_a?(::Mysql::Error) && MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message)) 264 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 267 def schema_column_type(db_type) 268 convert_tinyint_to_bool && db_type =~ /\Atinyint\(1\)/ ? :boolean : super 269 end