Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make :encoding option work on MySQL even if config file specifies dif…

…ferent encoding (Fixes #300, again)

After discussions with tmm1, this seems like the best way to work
around the bug in libmysqlclient, which makes configuration file
settings taken from READ_DEFAULT_GROUP override an explicitly
given SET_CHARSET_NAME option.  It uses a "SET NAMES" SQL query
after connection to attempt to ensure the connection will use
the correct encoding.  This is not safe across implicit
reconnects, but Sequel hasn't used those for some time.
  • Loading branch information...
commit 73046946c9e807474f09d435fa4e014ff4cfb1e8 1 parent 95b128e
@jeremyevans authored
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+=== 3.12.1 (2010-06-09)
+
+* Make :encoding option work on MySQL even if config file specifies different encoding (jeremyevans) (#300)
+
=== 3.12.0 (2010-06-01)
* Add a :deferrable option to foreign_key for creating deferrable foreign keys (hydrow)
View
15 lib/sequel/adapters/mysql.rb
@@ -99,6 +99,11 @@ def connect(server)
conn = Mysql.init
conn.options(Mysql::READ_DEFAULT_GROUP, opts[:config_default_group] || "client")
conn.options(Mysql::OPT_LOCAL_INFILE, opts[:config_local_infile]) if opts.has_key?(:config_local_infile)
+ if encoding = opts[:encoding] || opts[:charset]
+ # Set encoding before connecting so that the mysql driver knows what
+ # encoding we want to use, but this can be overridden by READ_DEFAULT_GROUP.
+ conn.options(Mysql::SET_CHARSET_NAME, encoding)
+ end
conn.real_connect(
opts[:host] || 'localhost',
opts[:user],
@@ -110,11 +115,11 @@ def connect(server)
Mysql::CLIENT_MULTI_STATEMENTS +
(opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
)
- if encoding = opts[:encoding] || opts[:charset]
- # Setting encoding before the connect appears not to work
- # with READ_DEFAULT_GROUP, so set it afterwards.
- conn.options(Mysql::SET_CHARSET_NAME, encoding)
- end
+ # Set encoding a slightly different way after connecting,
+ # in case the READ_DEFAULT_GROUP overrode the provided encoding.
+ # Doesn't work across implicit reconnects, but Sequel doesn't turn on
+ # that feature.
+ conn.query("set names #{literal(encoding.to_s)}") if encoding
# increase timeout so mysql server doesn't disconnect us
conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}")
View
2  lib/sequel/version.rb
@@ -1,7 +1,7 @@
module Sequel
MAJOR = 3
MINOR = 12
- TINY = 0
+ TINY = 1
VERSION = [MAJOR, MINOR, TINY].join('.')
Please sign in to comment.
Something went wrong with that request. Please try again.