Skip to content

Commit

Permalink
Make :encoding option work on MySQL even if config file specifies dif…
Browse files Browse the repository at this point in the history
…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
jeremyevans committed Jun 7, 2010
1 parent f2cfa04 commit a911579
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD

* Make :encoding option work on MySQL even if config file specifies different encoding (jeremyevans) (#300)

* Don't attempt to use class polymorphism in the class_table_inheritance plugin if no cti_key is defined (jeremyevans)

* Add a XmlSerializer plugin for serializing/deserializing model objects to/from XML (jeremyevans)
Expand Down
15 changes: 10 additions & 5 deletions lib/sequel/adapters/mysql.rb
Expand Up @@ -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],
Expand All @@ -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}")
Expand Down

0 comments on commit a911579

Please sign in to comment.