Skip to content

Commit c487270

Browse files
authored
Merge pull request #1549 from stevemandl/master
added tests and fixes for #1546
2 parents fce1a49 + 357887a commit c487270

File tree

10 files changed

+144
-71
lines changed

10 files changed

+144
-71
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-binary-multiple-results.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const fields1 = [
3939
catalog: 'def',
4040
characterSet: 63,
4141
columnType: 8,
42+
type: 8,
4243
decimals: 0,
4344
flags: 129,
4445
name: '1',
@@ -53,6 +54,7 @@ const nr_fields = [
5354
catalog: 'def',
5455
characterSet: 63,
5556
columnType: 3,
57+
type: 3,
5658
decimals: 0,
5759
flags: 0,
5860
name: 'test',

test/integration/connection/test-disconnects.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const server = common.createServer(
6161
characterSet: 63,
6262
columnLength: 1,
6363
columnType: 8,
64+
type: 8,
6465
flags: 129,
6566
decimals: 0
6667
}

test/integration/connection/test-execute-bind-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77
const date = new Date(2018, 2, 10, 15, 12, 34, 1234);
88

99
let rows;
10-
connection.execute('SELECT ? AS result', [date], (err, _rows) => {
10+
connection.execute('SELECT CAST(? AS DATETIME(6)) AS result', [date], (err, _rows) => {
1111
if (err) {
1212
throw err;
1313
}

test/integration/connection/test-execute-nocolumndef.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const expectedFields = [
5353
orgTable: '',
5454
characterSet: 63,
5555
columnType: 8,
56+
type: 8,
5657
flags: 161,
5758
decimals: 0
5859
},
@@ -65,6 +66,7 @@ const expectedFields = [
6566
orgTable: '',
6667
characterSet: 224,
6768
columnType: 253,
69+
type: 253,
6870
flags: 1,
6971
decimals: 31
7072
},
@@ -77,6 +79,7 @@ const expectedFields = [
7779
orgTable: '',
7880
characterSet: 224,
7981
columnType: 253,
82+
type: 253,
8083
flags: 0,
8184
decimals: 31
8285
},
@@ -89,6 +92,7 @@ const expectedFields = [
8992
orgTable: '',
9093
characterSet: 224,
9194
columnType: 250,
95+
type: 250,
9296
flags: 0,
9397
decimals: 31
9498
},
@@ -101,6 +105,7 @@ const expectedFields = [
101105
orgTable: '',
102106
characterSet: 224,
103107
columnType: 253,
108+
type: 253,
104109
flags: 0,
105110
decimals: 31
106111
},
@@ -113,6 +118,7 @@ const expectedFields = [
113118
orgTable: '',
114119
characterSet: 224,
115120
columnType: 253,
121+
type: 253,
116122
flags: 0,
117123
decimals: 31
118124
},
@@ -125,6 +131,7 @@ const expectedFields = [
125131
orgTable: '',
126132
characterSet: 224,
127133
columnType: 253,
134+
type: 253,
128135
flags: 0,
129136
decimals: 31
130137
},
@@ -137,6 +144,7 @@ const expectedFields = [
137144
orgTable: '',
138145
characterSet: 224,
139146
columnType: 253,
147+
type: 253,
140148
flags: 0,
141149
decimals: 31
142150
},
@@ -149,6 +157,7 @@ const expectedFields = [
149157
orgTable: '',
150158
characterSet: 224,
151159
columnType: 253,
160+
type: 253,
152161
flags: 0,
153162
decimals: 31
154163
},
@@ -161,6 +170,7 @@ const expectedFields = [
161170
orgTable: '',
162171
characterSet: 63,
163172
columnType: 8,
173+
type: 8,
164174
flags: 160,
165175
decimals: 0
166176
},
@@ -173,6 +183,7 @@ const expectedFields = [
173183
orgTable: '',
174184
characterSet: 63,
175185
columnType: 5,
186+
type: 5,
176187
flags: 128,
177188
decimals: 2
178189
},
@@ -185,6 +196,7 @@ const expectedFields = [
185196
orgTable: '',
186197
characterSet: 224,
187198
columnType: 253,
199+
type: 253,
188200
flags: 1,
189201
decimals: 31
190202
}

test/integration/connection/test-multiple-results.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const fields1 = [
3838
catalog: 'def',
3939
characterSet: 63,
4040
columnType: 8,
41+
type: 8,
4142
decimals: 0,
4243
flags: 129,
4344
name: '1',
@@ -52,6 +53,7 @@ const nr_fields = [
5253
catalog: 'def',
5354
characterSet: 63,
5455
columnType: 3,
56+
type: 3,
5557
decimals: 0,
5658
flags: 0,
5759
name: 'test',

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;

0 commit comments

Comments
 (0)