Skip to content
Open
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
4 changes: 3 additions & 1 deletion lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const validOptions = {
idleTimeout: 1,
Promise: 1,
queueLimit: 1,
waitForConnections: 1
waitForConnections: 1,
decimalStringTrimTrailingZero: 1,
};

class ConnectionConfig {
Expand Down Expand Up @@ -117,6 +118,7 @@ class ConnectionConfig {
this.stringifyObjects = options.stringifyObjects || false;
this.enableKeepAlive = options.enableKeepAlive !== false;
this.keepAliveInitialDelay = options.keepAliveInitialDelay || 0;
this.decimalStringTrimTrailingZero = options.decimalStringTrimTrailingZero || false;
if (
options.timezone &&
!/^(?:local|Z|[ +-]\d\d:\d\d)$/.test(options.timezone)
Expand Down
9 changes: 6 additions & 3 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class Packet {
);
}

readLengthCodedString(encoding) {
readLengthCodedString(encoding, trimTrailingZero = false) {
const len = this.readLengthCodedNumber();
// TODO: check manually first byte here to avoid polymorphic return type?
if (len === null) {
Expand All @@ -390,7 +390,10 @@ class Packet {
this.buffer,
encoding,
this.offset - len,
this.offset
this.offset,
{
trimTrailingZero
}
);
}

Expand Down Expand Up @@ -425,7 +428,7 @@ class Packet {
return StringParser.decode(
this.buffer,
encoding,
this.offset - len,
this.offset - len,
this.offset
);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/parsers/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ const Iconv = require('iconv-lite');

exports.decode = function(buffer, encoding, start, end, options) {
if (Buffer.isEncoding(encoding)) {
if(options?.trimTrailingZero) {
for(let i = end - 1; i >= start; i--) {
if(buffer[i] === 48) {
end--;
} else if(buffer[i] === 46) {
end--;
break;
} else {
break;
}
}
}
return buffer.toString(encoding, start, end);
}

Expand Down
7 changes: 6 additions & 1 deletion lib/parsers/text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
const bigNumberStrings = options.bigNumberStrings || config.bigNumberStrings;
const timezone = options.timezone || config.timezone;
const dateStrings = options.dateStrings || config.dateStrings;
const decimalStringTrimTrailingZero =
options.decimalStringTrimTrailingZero || config.decimalStringTrimTrailingZero;

switch (type) {
case Types.TINY:
Expand All @@ -40,7 +42,10 @@ function readCodeFor(type, charset, encodingExpr, config, options) {
if (config.decimalNumbers) {
return 'packet.parseLengthCodedFloat()';
}
return 'packet.readLengthCodedString("ascii")';
if(decimalStringTrimTrailingZero) {
return 'packet.readLengthCodedString("ascii", true)';
}
return 'packet.readLengthCodedString("ascii", false)';
case Types.DATE:
if (helpers.typeMatch(type, dateStrings, Types)) {
return 'packet.readLengthCodedString("ascii")';
Expand Down
1 change: 1 addition & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ exports.createConnection = function(args) {
namedPlaceholders: args && args.namedPlaceholders,
connectTimeout: args && args.connectTimeout,
ssl: (args && args.ssl) ?? config.ssl,
decimalStringTrimTrailingZero: (args && args.decimalStringTrimTrailingZero) ?? config.decimalStringTrimTrailingZero,
};

const conn = driver.createConnection(params);
Expand Down
8 changes: 8 additions & 0 deletions test/unit/connection/test-connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ assert.strictEqual(
).password,
'pass!%40$%%5E&*()%5Cword%3A'
);

assert.doesNotThrow(
() =>
new ConnectionConfig({
decimalStringTrimTrailingZero: true
}),
'Error, the constructor accepts an object but throws an exception'
);
44 changes: 44 additions & 0 deletions test/unit/parsers/test-decimal-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const assert = require('assert');
const common = require('../../common');

const connection = common.createConnection({decimalStringTrimTrailingZero: true});
connection.query('CREATE TEMPORARY TABLE t (a decimal(38,16), b varchar(39))');
connection.query('INSERT INTO t values(\'1.00\', \'1.00\')');
connection.query('INSERT INTO t values(\'1.01\', \'1.01\')');
connection.query('INSERT INTO t values(\'1.10\', \'1.10\')');
connection.query('INSERT INTO t values(\'1.010\', \'1.010\')');
connection.query('INSERT INTO t values(\'0.00\', \'0.00\')');
connection.query('INSERT INTO t values(\'100000.0000100000000000\', \'100000.0000100000000000\')');
connection.query('INSERT INTO t values(\'100000.0000000000000000\', \'100000.0000000000000000\')');


// JSON without encoding options - should result in unexpected behaviors
connection.query({
sql: 'SELECT * FROM t',
}, (err, rows) => {
assert.ifError(err);
assert.equal(rows[0].a, "1");
assert.equal(rows[0].b, "1.00");

assert.equal(rows[1].a, "1.01");
assert.equal(rows[1].b, "1.01");

assert.equal(rows[2].a, "1.1");
assert.equal(rows[2].b, "1.10");

assert.equal(rows[3].a, "1.01");
assert.equal(rows[3].b, "1.010");

assert.equal(rows[4].a, "0");
assert.equal(rows[4].b, "0.00");

assert.equal(rows[5].a, "100000.00001");
assert.equal(rows[5].b, "100000.0000100000000000");

assert.equal(rows[6].a, "100000");
assert.equal(rows[6].b, "100000.0000000000000000");
});

connection.end();
2 changes: 2 additions & 0 deletions typings/mysql/lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ export interface ConnectionOptions {
authPlugins?: {
[key: string]: AuthPlugin;
};

decimalStringTrimTrailingZero?: boolean;
}

declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
Expand Down