Skip to content

Implements JSON typecasting #1439

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions lib/protocol/packets/RowDataPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings,
: parser.parseLengthCodedString();
case Types.GEOMETRY:
return parser.parseGeometryValue();
case Types.JSON:
return JSON.parse(parser.parseLengthCodedString());
default:
return parser.parseLengthCodedString();
}
Expand Down
18 changes: 18 additions & 0 deletions test/integration/connection/test-change-user-charset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var assert = require('assert');
var common = require('../../common');

common.getTestConnection(function (err, connection) {
assert.ifError(err);

// should change charset
connection.changeUser({charset:'KOI8R_GENERAL_CI'}, function (err) {
assert.ifError(err);

connection.query('SHOW VARIABLES LIKE \'character_set_client\'', function (err, result) {
assert.ifError(err);
assert.strictEqual(result[0]['Value'], 'koi8r');

connection.destroy();
});
});
});
14 changes: 14 additions & 0 deletions test/integration/connection/test-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var path = require('path');
var assert = require('assert');
var common = require('../../common');
var lib = require(path.resolve(common.lib, '../index'));

assert.equal(
lib.format('SELECT * FROM ?? WHERE ?? = ?', [ 'table', 'property', 123 ]),
'SELECT * FROM `table` WHERE `property` = 123'
);

assert.equal(
lib.format('INSERT INTO ?? SET ?', [ 'table', { property: 123 } ]),
'INSERT INTO `table` SET `property` = 123'
);
9 changes: 8 additions & 1 deletion test/integration/connection/test-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ common.getTestConnection(function (err, connection) {
assert.ifError(err);
assert.deepEqual(rows, [{1: 1}]);
assert.equal(fields[0].name, '1');
connection.end(assert.ifError);

// this is a coverage test, it shuold perform exactly as the previous one
connection.query({ sql: 'SELECT ?' }, [ 1 ], function (err, rows, fields) {
assert.ifError(err);
assert.deepEqual(rows, [{1: 1}]);
assert.equal(fields[0].name, '1');
connection.end(assert.ifError);
});
});
});
3 changes: 2 additions & 1 deletion test/integration/connection/test-type-casting.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ var tests = [
{type: 'multipoint', insertRaw: "GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)')", expect: [{x:0, y:0}, {x:20, y:20}, {x:60, y:60}], deep: true},
{type: 'multilinestring', insertRaw: "GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))')", expect: [[{x:10,y:10},{x:20,y:20}],[{x:15,y:15},{x:30,y:15}]], deep: true},
{type: 'multipolygon', insertRaw: "GeomFromText('MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))')", expect: [[[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}]],[[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]]], deep: true},
{type: 'geometrycollection', insertRaw: "GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))')", expect: [{x:10,y:10},{x:30,y:30},[{x:15,y:15},{x:20,y:20}]], deep: true}
{type: 'geometrycollection', insertRaw: "GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))')", expect: [{x:10,y:10},{x:30,y:30},[{x:15,y:15},{x:20,y:20}]], deep: true},
{type: 'json', insert: { name: "mysql", data: [{ id: 5}, { id: 6}]}}
];

var table = 'type_casting';
Expand Down
11 changes: 11 additions & 0 deletions test/integration/connection/test-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var path = require('path');
var assert = require('assert');
var common = require('../../common');
var lib = require(path.resolve(common.lib, '../index'));
var types = require(path.resolve(common.lib, 'protocol/constants/types'));

assert.equal(typeof lib.Types, "object");

for (var k in types) {
assert.equal(lib.Types[k], types[k]);
}
14 changes: 12 additions & 2 deletions test/unit/connection/test-change-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ server.listen(common.fakeServerPort, function(err) {
assert.ifError(err);
assert.strictEqual(result[0]['CURRENT_USER()'], 'user_2@localhost');

connection.destroy();
server.destroy();
// should keep current user
connection.changeUser(function (err) {
assert.ifError(err);

connection.query('SELECT CURRENT_USER()', function (err, result) {
assert.ifError(err);
assert.strictEqual(result[0]['CURRENT_USER()'], 'user_2@localhost');

connection.destroy();
server.destroy();
});
});
});
});
});
Expand Down