Skip to content

Commit 115c8d6

Browse files
committed
Merge pull request #410 from simi/mysql_read_default_file
Allow read connection defaults from custom default file and group.
2 parents 926d579 + c998b1d commit 115c8d6

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tmp
1212
vendor
1313
lib/mysql2/mysql2.rb
1414
spec/configuration.yml
15+
spec/my.cnf

ext/mysql2/client.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
284284
VALUE rv;
285285
GET_CLIENT(self);
286286

287-
args.host = NIL_P(host) ? "localhost" : StringValuePtr(host);
287+
args.host = NIL_P(host) ? NULL : StringValuePtr(host);
288288
args.unix_socket = NIL_P(socket) ? NULL : StringValuePtr(socket);
289-
args.port = NIL_P(port) ? 3306 : NUM2INT(port);
289+
args.port = NIL_P(port) ? NULL : NUM2INT(port);
290290
args.user = NIL_P(user) ? NULL : StringValuePtr(user);
291291
args.passwd = NIL_P(pass) ? NULL : StringValuePtr(pass);
292292
args.db = NIL_P(database) ? NULL : StringValuePtr(database);
@@ -682,6 +682,7 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
682682
int result;
683683
void *retval = NULL;
684684
unsigned int intval = 0;
685+
const char * charval = NULL;
685686
my_bool boolval;
686687

687688
GET_CLIENT(self);
@@ -722,6 +723,16 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
722723
retval = &boolval;
723724
break;
724725

726+
case MYSQL_READ_DEFAULT_FILE:
727+
charval = (const char *)StringValuePtr(value);
728+
retval = charval;
729+
break;
730+
731+
case MYSQL_READ_DEFAULT_GROUP:
732+
charval = (const char *)StringValuePtr(value);
733+
retval = charval;
734+
break;
735+
725736
default:
726737
return Qfalse;
727738
}
@@ -1098,6 +1109,14 @@ static VALUE set_secure_auth(VALUE self, VALUE value) {
10981109
return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
10991110
}
11001111

1112+
static VALUE set_read_default_file(VALUE self, VALUE value) {
1113+
return _mysql_client_options(self, MYSQL_READ_DEFAULT_FILE, value);
1114+
}
1115+
1116+
static VALUE set_read_default_group(VALUE self, VALUE value) {
1117+
return _mysql_client_options(self, MYSQL_READ_DEFAULT_GROUP, value);
1118+
}
1119+
11011120
static VALUE initialize_ext(VALUE self) {
11021121
GET_CLIENT(self);
11031122

@@ -1167,6 +1186,8 @@ void init_mysql2_client() {
11671186
rb_define_private_method(cMysql2Client, "local_infile=", set_local_infile, 1);
11681187
rb_define_private_method(cMysql2Client, "charset_name=", set_charset_name, 1);
11691188
rb_define_private_method(cMysql2Client, "secure_auth=", set_secure_auth, 1);
1189+
rb_define_private_method(cMysql2Client, "default_file=", set_read_default_file, 1);
1190+
rb_define_private_method(cMysql2Client, "default_group=", set_read_default_group, 1);
11701191
rb_define_private_method(cMysql2Client, "ssl_set", set_ssl_options, 5);
11711192
rb_define_private_method(cMysql2Client, "initialize_ext", initialize_ext, 0);
11721193
rb_define_private_method(cMysql2Client, "connect", rb_connect, 7);

lib/mysql2/client.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Client
1010
:application_timezone => nil, # timezone Mysql2 will convert to before handing the object back to the caller
1111
:cache_rows => true, # tells Mysql2 to use it's internal row cache for results
1212
:connect_flags => REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
13-
:cast => true
13+
:cast => true,
14+
:default_file => nil,
15+
:default_group => nil
1416
}
1517

1618
def initialize(opts = {})
@@ -21,8 +23,7 @@ def initialize(opts = {})
2123

2224
initialize_ext
2325

24-
# Set MySQL connection options (each one is a call to mysql_options())
25-
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :secure_auth].each do |key|
26+
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth].each do |key|
2627
next unless opts.key?(key)
2728
case key
2829
when :reconnect, :local_infile, :secure_auth
@@ -49,8 +50,8 @@ def initialize(opts = {})
4950

5051
user = opts[:username] || opts[:user]
5152
pass = opts[:password] || opts[:pass]
52-
host = opts[:host] || opts[:hostname] || 'localhost'
53-
port = opts[:port] || 3306
53+
host = opts[:host] || opts[:hostname]
54+
port = opts[:port]
5455
database = opts[:database] || opts[:dbname] || opts[:db]
5556
socket = opts[:socket] || opts[:sock]
5657
flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]

spec/my.cnf.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[root]
2+
host=localhost
3+
user=LOCALUSERNAME
4+
password=
5+
6+
[client]
7+
host=localhost
8+
user=LOCALUSERNAME
9+
password=

spec/mysql2/client_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
require 'spec_helper'
33

44
describe Mysql2::Client do
5+
context "using defaults file" do
6+
let(:cnf_file) { File.expand_path('../../my.cnf', __FILE__) }
7+
8+
it "should not raise an exception for valid defaults group" do
9+
lambda {
10+
@client = Mysql2::Client.new(:default_file => cnf_file, :default_group => "test")
11+
}.should_not raise_error(Mysql2::Error)
12+
end
13+
14+
it "should not raise an exception without default group" do
15+
lambda {
16+
@client = Mysql2::Client.new(:default_file => cnf_file)
17+
}.should_not raise_error(Mysql2::Error)
18+
end
19+
end
20+
521
it "should raise an exception upon connection failure" do
622
lambda {
723
# The odd local host IP address forces the mysql client library to

tasks/rspec.rake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ file 'spec/configuration.yml' => 'spec/configuration.yml.example' do |task|
4040
sh "sed -i 's/LOCALUSERNAME/#{ENV['USER']}/' #{dst_path}"
4141
end
4242

43+
file 'spec/my.cnf' => 'spec/my.cnf.example' do |task|
44+
CLEAN.exclude task.name
45+
src_path = File.expand_path("../../#{task.prerequisites.first}", __FILE__)
46+
dst_path = File.expand_path("../../#{task.name}", __FILE__)
47+
cp src_path, dst_path
48+
sh "sed -i 's/LOCALUSERNAME/#{ENV['USER']}/' #{dst_path}"
49+
end
50+
4351
Rake::Task[:spec].prerequisites << :'spec/configuration.yml'
52+
Rake::Task[:spec].prerequisites << :'spec/my.cnf'

0 commit comments

Comments
 (0)