Skip to content

Commit 3c2af97

Browse files
committed
Add a setter method for init_command rather than a connect arg
1 parent 0aef65e commit 3c2af97

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

ext/mysql2/client.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ struct nogvl_connect_args {
6767
const char *user;
6868
const char *passwd;
6969
const char *db;
70-
const char *init_command;
7170
unsigned int port;
7271
const char *unix_socket;
7372
unsigned long client_flag;
@@ -157,10 +156,6 @@ static void *nogvl_connect(void *ptr) {
157156
struct nogvl_connect_args *args = ptr;
158157
MYSQL *client;
159158

160-
if (args->init_command != NULL) {
161-
mysql_options(args->mysql, MYSQL_INIT_COMMAND, args->init_command);
162-
}
163-
164159
client = mysql_real_connect(args->mysql, args->host,
165160
args->user, args->passwd,
166161
args->db, args->port, args->unix_socket,
@@ -327,7 +322,7 @@ static VALUE rb_mysql_info(VALUE self) {
327322
return rb_str;
328323
}
329324

330-
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags, VALUE init_command) {
325+
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags) {
331326
struct nogvl_connect_args args;
332327
VALUE rv;
333328
GET_CLIENT(self);
@@ -340,7 +335,6 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
340335
args.db = NIL_P(database) ? NULL : StringValuePtr(database);
341336
args.mysql = wrapper->client;
342337
args.client_flag = NUM2ULONG(flags);
343-
args.init_command = NIL_P(init_command) ? NULL : StringValuePtr(init_command);
344338

345339
rv = (VALUE) rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, 0);
346340
if (rv == Qfalse) {
@@ -786,6 +780,11 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
786780
retval = charval;
787781
break;
788782

783+
case MYSQL_INIT_COMMAND:
784+
charval = (const char *)StringValuePtr(value);
785+
retval = charval;
786+
break;
787+
789788
default:
790789
return Qfalse;
791790
}
@@ -1170,6 +1169,10 @@ static VALUE set_read_default_group(VALUE self, VALUE value) {
11701169
return _mysql_client_options(self, MYSQL_READ_DEFAULT_GROUP, value);
11711170
}
11721171

1172+
static VALUE set_init_command(VALUE self, VALUE value) {
1173+
return _mysql_client_options(self, MYSQL_INIT_COMMAND, value);
1174+
}
1175+
11731176
static VALUE initialize_ext(VALUE self) {
11741177
GET_CLIENT(self);
11751178

@@ -1241,9 +1244,10 @@ void init_mysql2_client() {
12411244
rb_define_private_method(cMysql2Client, "secure_auth=", set_secure_auth, 1);
12421245
rb_define_private_method(cMysql2Client, "default_file=", set_read_default_file, 1);
12431246
rb_define_private_method(cMysql2Client, "default_group=", set_read_default_group, 1);
1247+
rb_define_private_method(cMysql2Client, "init_command=", set_init_command, 1);
12441248
rb_define_private_method(cMysql2Client, "ssl_set", set_ssl_options, 5);
12451249
rb_define_private_method(cMysql2Client, "initialize_ext", initialize_ext, 0);
1246-
rb_define_private_method(cMysql2Client, "connect", rb_connect, 8);
1250+
rb_define_private_method(cMysql2Client, "connect", rb_connect, 7);
12471251

12481252
sym_id = ID2SYM(rb_intern("id"));
12491253
sym_version = ID2SYM(rb_intern("version"));

lib/mysql2/client.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(opts = {})
2323

2424
initialize_ext
2525

26-
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth].each do |key|
26+
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command].each do |key|
2727
next unless opts.key?(key)
2828
case key
2929
when :reconnect, :local_infile, :secure_auth
@@ -55,7 +55,6 @@ def initialize(opts = {})
5555
database = opts[:database] || opts[:dbname] || opts[:db]
5656
socket = opts[:socket] || opts[:sock]
5757
flags = opts[:flags] ? opts[:flags] | @query_options[:connect_flags] : @query_options[:connect_flags]
58-
init_command = opts[:init_command]
5958

6059
# Correct the data types before passing these values down to the C level
6160
user = user.to_s unless user.nil?
@@ -64,9 +63,8 @@ def initialize(opts = {})
6463
port = port.to_i unless port.nil?
6564
database = database.to_s unless database.nil?
6665
socket = socket.to_s unless socket.nil?
67-
init_command = init_command.to_s unless init_command.nil?
6866

69-
connect user, pass, host, port, database, socket, flags, init_command
67+
connect user, pass, host, port, database, socket, flags
7068
end
7169

7270
def self.default_query_options

spec/mysql2/client_spec.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ def connect *args
7474
Mysql2::Client::SECURE_CONNECTION)).should be_true
7575
end
7676

77-
it "should accept init_command" do
78-
klient = Class.new(Mysql2::Client) do
79-
attr_reader :connect_args
80-
def connect *args
81-
@connect_args ||= []
82-
@connect_args << args
83-
end
84-
end
85-
command = "SET @@session.something = 1"
86-
client = klient.new :init_command => command
87-
client.connect_args.last[7].should eq(command)
88-
end
89-
9077
it "should execute init command" do
9178
options = DatabaseCredentials['root'].dup
9279
options[:init_command] = "SET @something = 'setting_value';"

0 commit comments

Comments
 (0)