Skip to content

Commit 84bc37c

Browse files
committed
Use CLIENT_CONNECT_ATTRS flag to test the connection attributes feature
1 parent 929125b commit 84bc37c

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

ext/mysql2/client.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static VALUE rb_mysql_get_ssl_cipher(VALUE self)
400400
return rb_str;
401401
}
402402

403-
#ifdef HAVE_CONST_MYSQL_OPT_CONNECT_ATTR_ADD
403+
#ifdef CLIENT_CONNECT_ATTRS
404404
static int opt_connect_attr_add_i(VALUE key, VALUE value, VALUE arg)
405405
{
406406
mysql_client_wrapper *wrapper = (mysql_client_wrapper *)arg;
@@ -428,7 +428,7 @@ static VALUE rb_mysql_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VA
428428
args.mysql = wrapper->client;
429429
args.client_flag = NUM2ULONG(flags);
430430

431-
#ifdef HAVE_CONST_MYSQL_OPT_CONNECT_ATTR_ADD
431+
#ifdef CLIENT_CONNECT_ATTRS
432432
mysql_options(wrapper->client, MYSQL_OPT_CONNECT_ATTR_RESET, 0);
433433
rb_hash_foreach(conn_attrs, opt_connect_attr_add_i, (VALUE)wrapper);
434434
#endif
@@ -1558,6 +1558,16 @@ void init_mysql2_client() {
15581558
LONG2NUM(CLIENT_BASIC_FLAGS));
15591559
#endif
15601560

1561+
#ifdef CLIENT_CONNECT_ATTRS
1562+
rb_const_set(cMysql2Client, rb_intern("CONNECT_ATTRS"),
1563+
LONG2NUM(CLIENT_CONNECT_ATTRS));
1564+
#else
1565+
/* HACK because MySQL 5.5 and earlier don't define this constant,
1566+
* but we're using it in our default connection flags. */
1567+
rb_const_set(cMysql2Client, rb_intern("CONNECT_ATTRS"),
1568+
INT2NUM(0));
1569+
#endif
1570+
15611571
#if defined(FULL_SSL_MODE_SUPPORT) // MySQL 5.7.11 and above
15621572
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_DISABLED"), INT2NUM(SSL_MODE_DISABLED));
15631573
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_PREFERRED"), INT2NUM(SSL_MODE_PREFERRED));

ext/mysql2/extconf.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def add_ssl_defines(header)
103103
have_const('SERVER_QUERY_NO_GOOD_INDEX_USED', mysql_h)
104104
have_const('SERVER_QUERY_NO_INDEX_USED', mysql_h)
105105
have_const('SERVER_QUERY_WAS_SLOW', mysql_h)
106-
have_const('MYSQL_OPT_CONNECT_ATTR_ADD', mysql_h) # for mysql_options4
107106

108107
# This is our wishlist. We use whichever flags work on the host.
109108
# -Wall and -Wextra are included by default.

lib/mysql2/client.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.default_query_options
1111
database_timezone: :local, # timezone Mysql2 will assume datetime objects are stored in
1212
application_timezone: nil, # timezone Mysql2 will convert to before handing the object back to the caller
1313
cache_rows: true, # tells Mysql2 to use its internal row cache for results
14-
connect_flags: REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION,
14+
connect_flags: REMEMBER_OPTIONS | LONG_PASSWORD | LONG_FLAG | TRANSACTIONS | PROTOCOL_41 | SECURE_CONNECTION | CONNECT_ATTRS,
1515
cast: true,
1616
default_file: nil,
1717
default_group: nil,
@@ -64,11 +64,6 @@ def initialize(opts = {})
6464
# SSL verify is a connection flag rather than a mysql_ssl_set option
6565
flags |= SSL_VERIFY_SERVER_CERT if opts[:sslverify]
6666

67-
# Set default program_name in performance_schema.session_connect_attrs
68-
# and performance_schema.session_account_connect_attrs
69-
conn_attrs = opts[:connect_attrs] || {}
70-
conn_attrs[:program_name] = $PROGRAM_NAME unless conn_attrs.key?(:program_name)
71-
7267
if %i[user pass hostname dbname db sock].any? { |k| @query_options.key?(k) }
7368
warn "============= WARNING FROM mysql2 ============="
7469
warn "The options :user, :pass, :hostname, :dbname, :db, and :sock are deprecated and will be removed at some point in the future."
@@ -90,9 +85,7 @@ def initialize(opts = {})
9085
port = port.to_i unless port.nil?
9186
database = database.to_s unless database.nil?
9287
socket = socket.to_s unless socket.nil?
93-
conn_attrs = conn_attrs.each_with_object({}) do |(key, value), hash|
94-
hash[key.to_s] = value.to_s
95-
end
88+
conn_attrs = parse_connect_attrs(opts[:connect_attrs])
9689

9790
connect user, pass, host, port, database, socket, flags, conn_attrs
9891
end
@@ -122,6 +115,17 @@ def parse_flags_array(flags, initial = 0)
122115
end
123116
end
124117

118+
# Set default program_name in performance_schema.session_connect_attrs
119+
# and performance_schema.session_account_connect_attrs
120+
def parse_connect_attrs(conn_attrs)
121+
return {} if Mysql2::Client::CONNECT_ATTRS.zero?
122+
conn_attrs ||= {}
123+
conn_attrs[:program_name] ||= $PROGRAM_NAME
124+
conn_attrs.each_with_object({}) do |(key, value), hash|
125+
hash[key.to_s] = value.to_s
126+
end
127+
end
128+
125129
def query(sql, options = {})
126130
Thread.handle_interrupt(::Mysql2::Util::TIMEOUT_ERROR_CLASS => :never) do
127131
_query(sql, @query_options.merge(options))

spec/mysql2/client_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def connect(*args)
7979
Mysql2::Client::LONG_FLAG |
8080
Mysql2::Client::TRANSACTIONS |
8181
Mysql2::Client::PROTOCOL_41 |
82-
Mysql2::Client::SECURE_CONNECTION
82+
Mysql2::Client::SECURE_CONNECTION |
83+
Mysql2::Client::CONNECT_ATTRS
8384
expect(client.connect_args.last[6]).to eql(client_flags)
8485
end
8586

@@ -438,17 +439,17 @@ def run_gc
438439

439440
it "should set default program_name in connect_attrs" do
440441
client = new_client
441-
if Mysql2::Client.info[:version] < '5.6' || client.info[:version] < '5.6'
442-
pending('Both client and server versions must be MySQL 5.6 or later.')
442+
if Mysql2::Client::CONNECT_ATTRS.zero? || client.server_info[:version].match(/10.[01].\d+-MariaDB/)
443+
pending('Both client and server versions must be MySQL 5.6 or MariaDB 10.2 or later.')
443444
end
444445
result = client.query("SELECT attr_value FROM performance_schema.session_account_connect_attrs WHERE processlist_id = connection_id() AND attr_name = 'program_name'")
445446
expect(result.first['attr_value']).to eq($PROGRAM_NAME)
446447
end
447448

448449
it "should set custom connect_attrs" do
449450
client = new_client(connect_attrs: { program_name: 'my_program_name', foo: 'fooval', bar: 'barval' })
450-
if Mysql2::Client.info[:version] < '5.6' || client.info[:version] < '5.6'
451-
pending('Both client and server versions must be MySQL 5.6 or later.')
451+
if Mysql2::Client::CONNECT_ATTRS.zero? || client.server_info[:version].match(/10.[01].\d+-MariaDB/)
452+
pending('Both client and server versions must be MySQL 5.6 or MariaDB 10.2 or later.')
452453
end
453454
results = Hash[client.query("SELECT * FROM performance_schema.session_account_connect_attrs WHERE processlist_id = connection_id()").map { |x| x.values_at('ATTR_NAME', 'ATTR_VALUE') }]
454455
expect(results['program_name']).to eq('my_program_name')

0 commit comments

Comments
 (0)