Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default connection attributes #1825

Merged
merged 5 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const { URL } = require('url');
const ClientConstants = require('./constants/client');
const Charsets = require('./constants/charsets');
const { version } = require('../package.json')
let SSLProfiles = null;

const validOptions = {
Expand Down Expand Up @@ -169,7 +170,13 @@ class ConnectionConfig {
ConnectionConfig.getDefaultFlags(options),
options.flags || ''
);
this.connectAttributes = options.connectAttributes;
// Default connection attributes
// https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html
const defaultConnectAttributes = {
_client_name: 'Node-MySQL-2',
_client_version: version
};
this.connectAttributes = { ...defaultConnectAttributes, ...(options.connectAttributes || {})};
this.maxPreparedStatements = options.maxPreparedStatements || 16000;
}

Expand Down Expand Up @@ -217,17 +224,15 @@ class ConnectionConfig {
'SECURE_CONNECTION',
'MULTI_RESULTS',
'TRANSACTIONS',
'SESSION_TRACK'
'SESSION_TRACK',
'CONNECT_ATTRS'
];
if (options && options.multipleStatements) {
defaultFlags.push('MULTI_STATEMENTS');
}
defaultFlags.push('PLUGIN_AUTH');
defaultFlags.push('PLUGIN_AUTH_LENENC_CLIENT_DATA');

if (options && options.connectAttributes) {
defaultFlags.push('CONNECT_ATTRS');
}
return defaultFlags;
}

Expand Down
12 changes: 10 additions & 2 deletions test/integration/test-auth-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
const mysql = require('../../index.js');
const Command = require('../../lib/commands/command.js');
const Packets = require('../../lib/packets/index.js');
const {version} = require('../../package.json')

const assert = require('assert');

const connectAttributes = { foo: 'bar', baz: 'foo' };
const connectAttributes = {foo: 'bar', baz: 'foo'};

const defaultConnectAttributes = {
_client_name: 'Node-MySQL-2',
_client_version: version
};

let count = 0;

class TestAuthSwitchHandshake extends Command {
Expand Down Expand Up @@ -36,7 +43,8 @@ class TestAuthSwitchHandshake extends Command {
assert.equal(clientHelloReply.user, 'test_user');
assert.equal(clientHelloReply.database, 'test_database');
assert.equal(clientHelloReply.authPluginName, 'mysql_native_password');
assert.deepEqual(clientHelloReply.connectAttributes, connectAttributes);
assert.deepEqual(clientHelloReply.connectAttributes,
{...connectAttributes, ...defaultConnectAttributes});
const asr = new Packets.AuthSwitchRequest(this.args);
connection.writePacket(asr.toPacket());
return TestAuthSwitchHandshake.prototype.readClientAuthSwitchResponse;
Expand Down