Skip to content

Commit 71a0058

Browse files
committed
Merge pull request #427 from sodabrew/banzaiman_stringify
Connect to DB whose name consists entirely of numerics by coercing connection data types on the Ruby side.
2 parents 6615449 + ddb1c6e commit 71a0058

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/mysql2/client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def initialize(opts = {})
5656
socket = opts[:socket] || opts[:sock]
5757
flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]
5858

59+
# Correct the data types before passing these values down to the C level
60+
user = user.to_s unless user.nil?
61+
pass = pass.to_s unless pass.nil?
62+
host = host.to_s unless host.nil?
63+
port = port.to_i unless port.nil?
64+
database = database.to_s unless database.nil?
65+
socket = socket.to_s unless socket.nil?
66+
5967
connect user, pass, host, port, database, socket, flags
6068
end
6169

spec/configuration.yml.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ user:
99
username: LOCALUSERNAME
1010
password:
1111
database: mysql2_test
12+
13+
numericuser:
14+
host: localhost
15+
username: LOCALUSERNAME
16+
password:
17+
database: 12345

spec/mysql2/client_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def connect *args
104104
ssl_client.close
105105
end
106106

107+
it "should be able to connect to database with numeric-only name" do
108+
lambda {
109+
creds = DatabaseCredentials['numericuser']
110+
@client.query "CREATE DATABASE IF NOT EXISTS `#{creds['database']}`"
111+
@client.query "GRANT ALL ON `#{creds['database']}`.* TO #{creds['username']}@`#{creds['host']}`"
112+
client = Mysql2::Client.new creds
113+
@client.query "DROP DATABASE IF EXISTS `#{creds['database']}`"
114+
}.should_not raise_error
115+
end
116+
107117
it "should respond to #close" do
108118
@client.should respond_to(:close)
109119
end

0 commit comments

Comments
 (0)