Skip to content

Commit 1b37e12

Browse files
committed
added tests and fixes for #1546
1 parent fce1a49 commit 1b37e12

File tree

4 files changed

+123
-70
lines changed

4 files changed

+123
-70
lines changed

lib/constants/types.js

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
11
'use strict';
22

3+
module.exports = {
4+
0x00: 'DECIMAL', // aka DECIMAL
5+
0x01: 'TINY', // aka TINYINT, 1 byte
6+
0x02: 'SHORT', // aka SMALLINT, 2 bytes
7+
0x03: 'LONG', // aka INT, 4 bytes
8+
0x04: 'FLOAT', // aka FLOAT, 4-8 bytes
9+
0x05: 'DOUBLE', // aka DOUBLE, 8 bytes
10+
0x06: 'NULL', // NULL (used for prepared statements, I think)
11+
0x07: 'TIMESTAMP', // aka TIMESTAMP
12+
0x08: 'LONGLONG', // aka BIGINT, 8 bytes
13+
0x09: 'INT24', // aka MEDIUMINT, 3 bytes
14+
0x0a: 'DATE', // aka DATE
15+
0x0b: 'TIME', // aka TIME
16+
0x0c: 'DATETIME', // aka DATETIME
17+
0x0d: 'YEAR', // aka YEAR, 1 byte (don't ask)
18+
0x0e: 'NEWDATE', // aka ?
19+
0x0f: 'VARCHAR', // aka VARCHAR (?)
20+
0x10: 'BIT', // aka BIT, 1-8 byte
21+
0xf5: 'JSON',
22+
0xf6: 'NEWDECIMAL', // aka DECIMAL
23+
0xf7: 'ENUM', // aka ENUM
24+
0xf8: 'SET', // aka SET
25+
0xf9: 'TINY_BLOB', // aka TINYBLOB, TINYTEXT
26+
0xfa: 'MEDIUM_BLOB', // aka MEDIUMBLOB, MEDIUMTEXT
27+
0xfb: 'LONG_BLOB', // aka LONGBLOG, LONGTEXT
28+
0xfc: 'BLOB', // aka BLOB, TEXT
29+
0xfd: 'VAR_STRING', // aka VARCHAR, VARBINARY
30+
0xfe: 'STRING', // aka CHAR, BINARY
31+
0xff: 'GEOMETRY' // aka GEOMETRY
32+
};
33+
34+
335
// Manually extracted from mysql-5.5.23/include/mysql_com.h
436
// some more info here: http://dev.mysql.com/doc/refman/5.5/en/c-api-prepared-statement-type-codes.html
5-
exports.DECIMAL = 0x00; // aka DECIMAL (http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html)
6-
exports.TINY = 0x01; // aka TINYINT, 1 byte
7-
exports.SHORT = 0x02; // aka SMALLINT, 2 bytes
8-
exports.LONG = 0x03; // aka INT, 4 bytes
9-
exports.FLOAT = 0x04; // aka FLOAT, 4-8 bytes
10-
exports.DOUBLE = 0x05; // aka DOUBLE, 8 bytes
11-
exports.NULL = 0x06; // NULL (used for prepared statements, I think)
12-
exports.TIMESTAMP = 0x07; // aka TIMESTAMP
13-
exports.LONGLONG = 0x08; // aka BIGINT, 8 bytes
14-
exports.INT24 = 0x09; // aka MEDIUMINT, 3 bytes
15-
exports.DATE = 0x0a; // aka DATE
16-
exports.TIME = 0x0b; // aka TIME
17-
exports.DATETIME = 0x0c; // aka DATETIME
18-
exports.YEAR = 0x0d; // aka YEAR, 1 byte (don't ask)
19-
exports.NEWDATE = 0x0e; // aka ?
20-
exports.VARCHAR = 0x0f; // aka VARCHAR (?)
21-
exports.BIT = 0x10; // aka BIT, 1-8 byte
22-
exports.JSON = 0xf5;
23-
exports.NEWDECIMAL = 0xf6; // aka DECIMAL
24-
exports.ENUM = 0xf7; // aka ENUM
25-
exports.SET = 0xf8; // aka SET
26-
exports.TINY_BLOB = 0xf9; // aka TINYBLOB, TINYTEXT
27-
exports.MEDIUM_BLOB = 0xfa; // aka MEDIUMBLOB, MEDIUMTEXT
28-
exports.LONG_BLOB = 0xfb; // aka LONGBLOG, LONGTEXT
29-
exports.BLOB = 0xfc; // aka BLOB, TEXT
30-
exports.VAR_STRING = 0xfd; // aka VARCHAR, VARBINARY
31-
exports.STRING = 0xfe; // aka CHAR, BINARY
32-
exports.GEOMETRY = 0xff; // aka GEOMETRY
37+
module.exports.DECIMAL = 0x00; // aka DECIMAL (http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html)
38+
module.exports.TINY = 0x01; // aka TINYINT, 1 byte
39+
module.exports.SHORT = 0x02; // aka SMALLINT, 2 bytes
40+
module.exports.LONG = 0x03; // aka INT, 4 bytes
41+
module.exports.FLOAT = 0x04; // aka FLOAT, 4-8 bytes
42+
module.exports.DOUBLE = 0x05; // aka DOUBLE, 8 bytes
43+
module.exports.NULL = 0x06; // NULL (used for prepared statements, I think)
44+
module.exports.TIMESTAMP = 0x07; // aka TIMESTAMP
45+
module.exports.LONGLONG = 0x08; // aka BIGINT, 8 bytes
46+
module.exports.INT24 = 0x09; // aka MEDIUMINT, 3 bytes
47+
module.exports.DATE = 0x0a; // aka DATE
48+
module.exports.TIME = 0x0b; // aka TIME
49+
module.exports.DATETIME = 0x0c; // aka DATETIME
50+
module.exports.YEAR = 0x0d; // aka YEAR, 1 byte (don't ask)
51+
module.exports.NEWDATE = 0x0e; // aka ?
52+
module.exports.VARCHAR = 0x0f; // aka VARCHAR (?)
53+
module.exports.BIT = 0x10; // aka BIT, 1-8 byte
54+
module.exports.JSON = 0xf5;
55+
module.exports.NEWDECIMAL = 0xf6; // aka DECIMAL
56+
module.exports.ENUM = 0xf7; // aka ENUM
57+
module.exports.SET = 0xf8; // aka SET
58+
module.exports.TINY_BLOB = 0xf9; // aka TINYBLOB, TINYTEXT
59+
module.exports.MEDIUM_BLOB = 0xfa; // aka MEDIUMBLOB, MEDIUMTEXT
60+
module.exports.LONG_BLOB = 0xfb; // aka LONGBLOG, LONGTEXT
61+
module.exports.BLOB = 0xfc; // aka BLOB, TEXT
62+
module.exports.VAR_STRING = 0xfd; // aka VARCHAR, VARBINARY
63+
module.exports.STRING = 0xfe; // aka CHAR, BINARY
64+
module.exports.GEOMETRY = 0xff; // aka GEOMETRY

lib/packets/column_definition.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ColumnDefinition {
5252
);
5353
this.columnLength = packet.readInt32();
5454
this.columnType = packet.readInt8();
55+
this.type = this.columnType;
5556
this.flags = packet.readInt16();
5657
this.decimals = packet.readInt8();
5758
}
@@ -67,6 +68,7 @@ class ColumnDefinition {
6768
characterSet: this.characterSet,
6869
columnLength: this.columnLength,
6970
columnType: this.columnType,
71+
type: this.columnType,
7072
flags: this.flags,
7173
decimals: this.decimals
7274
};

test/integration/connection/test-type-casting.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const common = require('../../common');
4+
const driver = require('../../../index.js'); //needed to check driver.Types
45
const connection = common.createConnection();
56
const assert = require('assert');
67

@@ -38,17 +39,26 @@ connection.query('select 1', waitConnectErr => {
3839
connection.query(`INSERT INTO ${table} SET${inserts.join(',\n')}`);
3940

4041
let row;
41-
connection.query('SELECT * FROM type_casting', (err, rows) => {
42+
let fieldData; // to lookup field types
43+
connection.query(`SELECT * FROM ${table}`, (err, rows, fields) => {
4244
if (err) {
4345
throw err;
4446
}
4547

4648
row = rows[0];
49+
// build a fieldName: fieldType lookup table
50+
fieldData = fields.reduce((a,v) => {
51+
a[v['name']] = v['type'];
52+
return a;
53+
}, {} );
4754
connection.end();
4855
});
4956

5057
process.on('exit', () => {
5158
tests.forEach(test => {
59+
// check that the column type matches the type name stored in driver.Types
60+
const columnType = fieldData[test.columnName];
61+
assert.equal(test.columnType === driver.Types[columnType], true, test.columnName);
5262
let expected = test.expect || test.insert;
5363
let got = row[test.columnName];
5464
let message;

test/integration/connection/type-casting-tests.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,44 @@ module.exports = function(connection) {
77
const stPrefix = serverVersion[0] === '8' ? 'ST_' : '';
88

99
return [
10-
{ type: 'decimal(4,3)', insert: '1.234' },
10+
{ type: 'decimal(4,3)', insert: '1.234', columnType: 'NEWDECIMAL' },
1111
// {type: 'decimal(3,3)', insert: 0.33},
12-
{ type: 'tinyint', insert: 1 },
13-
{ type: 'smallint', insert: 2 },
14-
{ type: 'int', insert: 3 },
15-
{ type: 'float', insert: 4.5 },
16-
{ type: 'double', insert: 5.5 },
17-
{ type: 'bigint', insert: '6', expect: 6 },
18-
{ type: 'bigint', insert: 6 },
19-
{ type: 'mediumint', insert: 7 },
20-
{ type: 'year', insert: 2012 },
21-
{ type: 'timestamp', insert: new Date('2012-05-12 11:00:23') },
22-
{ type: 'datetime', insert: new Date('2012-05-12 12:00:23') },
23-
{ type: 'date', insert: new Date('2012-05-12 00:00:00') },
24-
{ type: 'time', insert: '13:13:23' },
25-
{ type: 'time', insert: '-13:13:23' },
26-
{ type: 'time', insert: '413:13:23' },
27-
{ type: 'time', insert: '-413:13:23' },
28-
{ type: 'binary(4)', insert: Buffer.from([0, 1, 254, 255]) },
29-
{ type: 'varbinary(4)', insert: Buffer.from([0, 1, 254, 255]) },
30-
{ type: 'tinyblob', insert: Buffer.from([0, 1, 254, 255]) },
31-
{ type: 'mediumblob', insert: Buffer.from([0, 1, 254, 255]) },
32-
{ type: 'longblob', insert: Buffer.from([0, 1, 254, 255]) },
33-
{ type: 'blob', insert: Buffer.from([0, 1, 254, 255]) },
34-
{ type: 'bit(32)', insert: Buffer.from([0, 1, 254, 255]) },
35-
{ type: 'char(5)', insert: 'Hello' },
36-
{ type: 'varchar(5)', insert: 'Hello' },
37-
{ type: 'varchar(3) character set utf8 collate utf8_bin', insert: 'bin' },
38-
{ type: 'tinytext', insert: 'Hello World' },
39-
{ type: 'mediumtext', insert: 'Hello World' },
40-
{ type: 'longtext', insert: 'Hello World' },
41-
{ type: 'text', insert: 'Hello World' },
12+
{ type: 'tinyint', insert: 1, columnType: 'TINY' },
13+
{ type: 'smallint', insert: 2, columnType: 'SHORT' },
14+
{ type: 'int', insert: 3, columnType: 'LONG' },
15+
{ type: 'float', insert: 4.5, columnType: 'FLOAT' },
16+
{ type: 'double', insert: 5.5, columnType: 'DOUBLE' },
17+
{ type: 'bigint', insert: '6', expect: 6, columnType: 'LONGLONG' },
18+
{ type: 'bigint', insert: 6, columnType: 'LONGLONG' },
19+
{ type: 'mediumint', insert: 7, columnType: 'INT24' },
20+
{ type: 'year', insert: 2012, columnType: 'YEAR' },
21+
{ type: 'timestamp', insert: new Date('2012-05-12 11:00:23'), columnType: 'TIMESTAMP' },
22+
{ type: 'datetime', insert: new Date('2012-05-12 12:00:23'), columnType: 'DATETIME' },
23+
{ type: 'date', insert: new Date('2012-05-12 00:00:00'), columnType: 'DATE' },
24+
{ type: 'time', insert: '13:13:23', columnType: 'TIME' },
25+
{ type: 'time', insert: '-13:13:23', columnType: 'TIME' },
26+
{ type: 'time', insert: '413:13:23', columnType: 'TIME' },
27+
{ type: 'time', insert: '-413:13:23', columnType: 'TIME' },
28+
{ type: 'binary(4)', insert: Buffer.from([0, 1, 254, 255]), columnType: 'STRING' },
29+
{ type: 'varbinary(4)', insert: Buffer.from([0, 1, 254, 255]), columnType: 'VAR_STRING' },
30+
{ type: 'tinyblob', insert: Buffer.from([0, 1, 254, 255]), columnType: 'BLOB' },
31+
{ type: 'mediumblob', insert: Buffer.from([0, 1, 254, 255]), columnType: 'BLOB' },
32+
{ type: 'longblob', insert: Buffer.from([0, 1, 254, 255]), columnType: 'BLOB' },
33+
{ type: 'blob', insert: Buffer.from([0, 1, 254, 255]), columnType: 'BLOB' },
34+
{ type: 'bit(32)', insert: Buffer.from([0, 1, 254, 255]), columnType: 'BIT' },
35+
{ type: 'char(5)', insert: 'Hello', columnType: 'STRING' },
36+
{ type: 'varchar(5)', insert: 'Hello', columnType: 'VAR_STRING' },
37+
{ type: 'varchar(3) character set utf8 collate utf8_bin', insert: 'bin', columnType: 'VAR_STRING' },
38+
{ type: 'tinytext', insert: 'Hello World', columnType: 'BLOB' },
39+
{ type: 'mediumtext', insert: 'Hello World', columnType: 'BLOB' },
40+
{ type: 'longtext', insert: 'Hello World', columnType: 'BLOB' },
41+
{ type: 'text', insert: 'Hello World', columnType: 'BLOB' },
4242
{
4343
type: 'point',
4444
insertRaw: 'POINT(1.2,-3.4)',
4545
expect: { x: 1.2, y: -3.4 },
46-
deep: true
46+
deep: true,
47+
columnType: 'GEOMETRY'
4748
},
4849
{
4950
type: 'point',
@@ -56,14 +57,16 @@ module.exports = function(connection) {
5657
return `${stPrefix}GeomFromWKB(${connection.escape(buffer)})`;
5758
})(),
5859
expect: { x: -5.6, y: 10.23 },
59-
deep: true
60+
deep: true,
61+
columnType: 'GEOMETRY'
6062
},
61-
{ type: 'point', insertRaw: '', insert: null, expect: null },
63+
{ type: 'point', insertRaw: '', insert: null, expect: null, columnType: 'GEOMETRY' },
6264
{
6365
type: 'linestring',
6466
insertRaw: 'LINESTRING(POINT(1.2,-3.4),POINT(-5.6,10.23),POINT(0.2,0.7))',
6567
expect: [{ x: 1.2, y: -3.4 }, { x: -5.6, y: 10.23 }, { x: 0.2, y: 0.7 }],
66-
deep: true
68+
deep: true,
69+
columnType: 'GEOMETRY'
6770
},
6871
{
6972
type: 'polygon',
@@ -84,19 +87,22 @@ module.exports = function(connection) {
8487
{ x: 5, y: 5 }
8588
]
8689
],
87-
deep: true
90+
deep: true,
91+
columnType: 'GEOMETRY'
8892
},
8993
{
9094
type: 'geometry',
9195
insertRaw: 'POINT(1.2,-3.4)',
9296
expect: { x: 1.2, y: -3.4 },
93-
deep: true
97+
deep: true,
98+
columnType: 'GEOMETRY'
9499
},
95100
{
96101
type: 'multipoint',
97102
insertRaw: `${stPrefix}GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)')`,
98103
expect: [{ x: 0, y: 0 }, { x: 20, y: 20 }, { x: 60, y: 60 }],
99-
deep: true
104+
deep: true,
105+
columnType: 'GEOMETRY'
100106
},
101107
{
102108
type: 'multilinestring',
@@ -105,7 +111,8 @@ module.exports = function(connection) {
105111
[{ x: 10, y: 10 }, { x: 20, y: 20 }],
106112
[{ x: 15, y: 15 }, { x: 30, y: 15 }]
107113
],
108-
deep: true
114+
deep: true,
115+
columnType: 'GEOMETRY'
109116
},
110117
{
111118
type: 'multipolygon',
@@ -130,7 +137,8 @@ module.exports = function(connection) {
130137
]
131138
]
132139
],
133-
deep: true
140+
deep: true,
141+
columnType: 'GEOMETRY'
134142
},
135143
{
136144
type: 'geometrycollection',
@@ -140,7 +148,8 @@ module.exports = function(connection) {
140148
{ x: 31, y: 30 },
141149
[{ x: 15, y: 15 }, { x: 20, y: 20 }]
142150
],
143-
deep: true
151+
deep: true,
152+
columnType: 'GEOMETRY'
144153
}
145154
];
146155
};

0 commit comments

Comments
 (0)