oceanbase 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/License +19 -0
- package/README.md +250 -0
- package/index.d.ts +1 -0
- package/index.js +77 -0
- package/lib/auth_41.js +95 -0
- package/lib/auth_plugins/caching_sha2_password.js +108 -0
- package/lib/auth_plugins/caching_sha2_password.md +18 -0
- package/lib/auth_plugins/index.js +8 -0
- package/lib/auth_plugins/mysql_clear_password.js +17 -0
- package/lib/auth_plugins/mysql_native_password.js +34 -0
- package/lib/auth_plugins/sha256_password.js +68 -0
- package/lib/base/connection.js +978 -0
- package/lib/base/pool.js +237 -0
- package/lib/base/pool_connection.js +70 -0
- package/lib/commands/auth_switch.js +111 -0
- package/lib/commands/binlog_dump.js +109 -0
- package/lib/commands/change_user.js +68 -0
- package/lib/commands/client_handshake.js +241 -0
- package/lib/commands/close_statement.js +18 -0
- package/lib/commands/command.js +54 -0
- package/lib/commands/execute.js +112 -0
- package/lib/commands/index.js +27 -0
- package/lib/commands/ping.js +36 -0
- package/lib/commands/prepare.js +143 -0
- package/lib/commands/query.js +366 -0
- package/lib/commands/quit.js +29 -0
- package/lib/commands/register_slave.js +27 -0
- package/lib/commands/server_handshake.js +203 -0
- package/lib/compressed_protocol.js +127 -0
- package/lib/connection.js +12 -0
- package/lib/connection_config.js +326 -0
- package/lib/constants/charset_encodings.js +316 -0
- package/lib/constants/charsets.js +317 -0
- package/lib/constants/client.js +40 -0
- package/lib/constants/commands.js +36 -0
- package/lib/constants/cursor.js +8 -0
- package/lib/constants/encoding_charset.js +50 -0
- package/lib/constants/errors.js +3973 -0
- package/lib/constants/field_flags.js +20 -0
- package/lib/constants/server_status.js +44 -0
- package/lib/constants/session_track.js +11 -0
- package/lib/constants/ssl_profiles.js +11 -0
- package/lib/constants/types.js +64 -0
- package/lib/create_connection.js +10 -0
- package/lib/create_pool.js +10 -0
- package/lib/create_pool_cluster.js +9 -0
- package/lib/helpers.js +86 -0
- package/lib/packet_parser.js +195 -0
- package/lib/packets/auth_next_factor.js +35 -0
- package/lib/packets/auth_switch_request.js +38 -0
- package/lib/packets/auth_switch_request_more_data.js +33 -0
- package/lib/packets/auth_switch_response.js +30 -0
- package/lib/packets/binary_row.js +95 -0
- package/lib/packets/binlog_dump.js +33 -0
- package/lib/packets/binlog_query_statusvars.js +115 -0
- package/lib/packets/change_user.js +97 -0
- package/lib/packets/close_statement.js +21 -0
- package/lib/packets/column_definition.js +291 -0
- package/lib/packets/execute.js +214 -0
- package/lib/packets/handshake.js +112 -0
- package/lib/packets/handshake_response.js +144 -0
- package/lib/packets/index.js +152 -0
- package/lib/packets/packet.js +931 -0
- package/lib/packets/prepare_statement.js +27 -0
- package/lib/packets/prepared_statement_header.js +16 -0
- package/lib/packets/query.js +27 -0
- package/lib/packets/register_slave.js +46 -0
- package/lib/packets/resultset_header.js +124 -0
- package/lib/packets/ssl_request.js +25 -0
- package/lib/packets/text_row.js +47 -0
- package/lib/parsers/binary_parser.js +235 -0
- package/lib/parsers/parser_cache.js +68 -0
- package/lib/parsers/static_binary_parser.js +213 -0
- package/lib/parsers/static_text_parser.js +152 -0
- package/lib/parsers/string.js +50 -0
- package/lib/parsers/text_parser.js +214 -0
- package/lib/pool.js +12 -0
- package/lib/pool_cluster.js +369 -0
- package/lib/pool_config.js +30 -0
- package/lib/pool_connection.js +12 -0
- package/lib/promise/connection.js +222 -0
- package/lib/promise/inherit_events.js +27 -0
- package/lib/promise/make_done_cb.js +19 -0
- package/lib/promise/pool.js +112 -0
- package/lib/promise/pool_cluster.js +54 -0
- package/lib/promise/pool_connection.js +19 -0
- package/lib/promise/prepared_statement_info.js +32 -0
- package/lib/results_stream.js +38 -0
- package/lib/server.js +37 -0
- package/package.json +80 -0
- package/promise.d.ts +131 -0
- package/promise.js +202 -0
- package/typings/mysql/LICENSE.txt +15 -0
- package/typings/mysql/index.d.ts +95 -0
- package/typings/mysql/info.txt +1 -0
- package/typings/mysql/lib/Auth.d.ts +30 -0
- package/typings/mysql/lib/Connection.d.ts +453 -0
- package/typings/mysql/lib/Pool.d.ts +69 -0
- package/typings/mysql/lib/PoolCluster.d.ts +90 -0
- package/typings/mysql/lib/PoolConnection.d.ts +10 -0
- package/typings/mysql/lib/Server.d.ts +11 -0
- package/typings/mysql/lib/constants/CharsetToEncoding.d.ts +8 -0
- package/typings/mysql/lib/constants/Charsets.d.ts +326 -0
- package/typings/mysql/lib/constants/Types.d.ts +70 -0
- package/typings/mysql/lib/constants/index.d.ts +5 -0
- package/typings/mysql/lib/parsers/ParserCache.d.ts +4 -0
- package/typings/mysql/lib/parsers/index.d.ts +18 -0
- package/typings/mysql/lib/parsers/typeCast.d.ts +54 -0
- package/typings/mysql/lib/protocol/packets/Field.d.ts +10 -0
- package/typings/mysql/lib/protocol/packets/FieldPacket.d.ts +27 -0
- package/typings/mysql/lib/protocol/packets/OkPacket.d.ts +23 -0
- package/typings/mysql/lib/protocol/packets/ProcedurePacket.d.ts +13 -0
- package/typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts +18 -0
- package/typings/mysql/lib/protocol/packets/RowDataPacket.d.ts +9 -0
- package/typings/mysql/lib/protocol/packets/index.d.ts +28 -0
- package/typings/mysql/lib/protocol/packets/params/ErrorPacketParams.d.ts +6 -0
- package/typings/mysql/lib/protocol/packets/params/OkPacketParams.d.ts +9 -0
- package/typings/mysql/lib/protocol/sequences/ExecutableBase.d.ts +40 -0
- package/typings/mysql/lib/protocol/sequences/Prepare.d.ts +65 -0
- package/typings/mysql/lib/protocol/sequences/Query.d.ts +170 -0
- package/typings/mysql/lib/protocol/sequences/QueryableBase.d.ts +40 -0
- package/typings/mysql/lib/protocol/sequences/Sequence.d.ts +5 -0
- package/typings/mysql/lib/protocol/sequences/promise/ExecutableBase.d.ts +21 -0
- package/typings/mysql/lib/protocol/sequences/promise/QueryableBase.d.ts +21 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Types = require('../constants/types');
|
|
4
|
+
const Packet = require('../packets/packet');
|
|
5
|
+
|
|
6
|
+
const binaryReader = new Array(256);
|
|
7
|
+
|
|
8
|
+
class BinaryRow {
|
|
9
|
+
constructor(columns) {
|
|
10
|
+
this.columns = columns || [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static toPacket(columns, encoding) {
|
|
14
|
+
// throw new Error('Not implemented');
|
|
15
|
+
const sequenceId = 0; // TODO remove, this is calculated now in connecton
|
|
16
|
+
let length = 0;
|
|
17
|
+
columns.forEach((val) => {
|
|
18
|
+
if (val === null || typeof val === 'undefined') {
|
|
19
|
+
++length;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
length += Packet.lengthCodedStringLength(val.toString(10), encoding);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
length = length + 2;
|
|
26
|
+
|
|
27
|
+
const buffer = Buffer.allocUnsafe(length + 4);
|
|
28
|
+
const packet = new Packet(sequenceId, buffer, 0, length + 4);
|
|
29
|
+
packet.offset = 4;
|
|
30
|
+
|
|
31
|
+
packet.writeInt8(0);
|
|
32
|
+
|
|
33
|
+
let bitmap = 0;
|
|
34
|
+
let bitValue = 1;
|
|
35
|
+
columns.forEach((parameter) => {
|
|
36
|
+
if (parameter.type === Types.NULL) {
|
|
37
|
+
bitmap += bitValue;
|
|
38
|
+
}
|
|
39
|
+
bitValue *= 2;
|
|
40
|
+
if (bitValue === 256) {
|
|
41
|
+
packet.writeInt8(bitmap);
|
|
42
|
+
bitmap = 0;
|
|
43
|
+
bitValue = 1;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
if (bitValue !== 1) {
|
|
47
|
+
packet.writeInt8(bitmap);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
columns.forEach((val) => {
|
|
51
|
+
if (val === null) {
|
|
52
|
+
packet.writeNull();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (typeof val === 'undefined') {
|
|
56
|
+
packet.writeInt8(0);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
packet.writeLengthCodedString(val.toString(10), encoding);
|
|
60
|
+
});
|
|
61
|
+
return packet;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// TODO: complete list of types...
|
|
65
|
+
static fromPacket(fields, packet) {
|
|
66
|
+
const columns = new Array(fields.length);
|
|
67
|
+
packet.readInt8(); // TODO check it's 0
|
|
68
|
+
const nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
|
|
69
|
+
// TODO: read and interpret null bitmap
|
|
70
|
+
packet.skip(nullBitmapLength);
|
|
71
|
+
for (let i = 0; i < columns.length; ++i) {
|
|
72
|
+
columns[i] = binaryReader[fields[i].columnType].apply(packet);
|
|
73
|
+
}
|
|
74
|
+
return new BinaryRow(columns);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// TODO: replace with constants.MYSQL_TYPE_*
|
|
79
|
+
binaryReader[Types.DECIMAL] = Packet.prototype.readLengthCodedString;
|
|
80
|
+
binaryReader[1] = Packet.prototype.readInt8; // tiny
|
|
81
|
+
binaryReader[2] = Packet.prototype.readInt16; // short
|
|
82
|
+
binaryReader[3] = Packet.prototype.readInt32; // long
|
|
83
|
+
binaryReader[4] = Packet.prototype.readFloat; // float
|
|
84
|
+
binaryReader[5] = Packet.prototype.readDouble; // double
|
|
85
|
+
binaryReader[6] = Packet.prototype.assertInvalid; // null, should be skipped vie null bitmap
|
|
86
|
+
binaryReader[7] = Packet.prototype.readTimestamp; // timestamp, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIMESTAMP
|
|
87
|
+
binaryReader[8] = Packet.prototype.readInt64; // long long
|
|
88
|
+
binaryReader[9] = Packet.prototype.readInt32; // int24
|
|
89
|
+
binaryReader[10] = Packet.prototype.readTimestamp; // date
|
|
90
|
+
binaryReader[11] = Packet.prototype.readTime; // time, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIME
|
|
91
|
+
binaryReader[12] = Packet.prototype.readDateTime; // datetime, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_DATETIME
|
|
92
|
+
binaryReader[13] = Packet.prototype.readInt16; // year
|
|
93
|
+
binaryReader[Types.VAR_STRING] = Packet.prototype.readLengthCodedString; // var string
|
|
94
|
+
|
|
95
|
+
module.exports = BinaryRow;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// http://dev.mysql.com/doc/internals/en/com-binlog-dump.html#packet-COM_BINLOG_DUMP
|
|
4
|
+
|
|
5
|
+
const Packet = require('../packets/packet');
|
|
6
|
+
const CommandCodes = require('../constants/commands');
|
|
7
|
+
|
|
8
|
+
// TODO: add flag to constants
|
|
9
|
+
// 0x01 - BINLOG_DUMP_NON_BLOCK
|
|
10
|
+
// send EOF instead of blocking
|
|
11
|
+
class BinlogDump {
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
this.binlogPos = opts.binlogPos || 0;
|
|
14
|
+
this.serverId = opts.serverId || 0;
|
|
15
|
+
this.flags = opts.flags || 0;
|
|
16
|
+
this.filename = opts.filename || '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
toPacket() {
|
|
20
|
+
const length = 15 + Buffer.byteLength(this.filename, 'utf8'); // TODO: should be ascii?
|
|
21
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
22
|
+
const packet = new Packet(0, buffer, 0, length);
|
|
23
|
+
packet.offset = 4;
|
|
24
|
+
packet.writeInt8(CommandCodes.BINLOG_DUMP);
|
|
25
|
+
packet.writeInt32(this.binlogPos);
|
|
26
|
+
packet.writeInt16(this.flags);
|
|
27
|
+
packet.writeInt32(this.serverId);
|
|
28
|
+
packet.writeString(this.filename);
|
|
29
|
+
return packet;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = BinlogDump;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// http://dev.mysql.com/doc/internals/en/query-event.html
|
|
4
|
+
|
|
5
|
+
const keys = {
|
|
6
|
+
FLAGS2: 0,
|
|
7
|
+
SQL_MODE: 1,
|
|
8
|
+
CATALOG: 2,
|
|
9
|
+
AUTO_INCREMENT: 3,
|
|
10
|
+
CHARSET: 4,
|
|
11
|
+
TIME_ZONE: 5,
|
|
12
|
+
CATALOG_NZ: 6,
|
|
13
|
+
LC_TIME_NAMES: 7,
|
|
14
|
+
CHARSET_DATABASE: 8,
|
|
15
|
+
TABLE_MAP_FOR_UPDATE: 9,
|
|
16
|
+
MASTER_DATA_WRITTEN: 10,
|
|
17
|
+
INVOKERS: 11,
|
|
18
|
+
UPDATED_DB_NAMES: 12,
|
|
19
|
+
MICROSECONDS: 3,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = function parseStatusVars(buffer) {
|
|
23
|
+
const result = {};
|
|
24
|
+
let offset = 0;
|
|
25
|
+
let key, length, prevOffset;
|
|
26
|
+
while (offset < buffer.length) {
|
|
27
|
+
key = buffer[offset++];
|
|
28
|
+
switch (key) {
|
|
29
|
+
case keys.FLAGS2:
|
|
30
|
+
result.flags = buffer.readUInt32LE(offset);
|
|
31
|
+
offset += 4;
|
|
32
|
+
break;
|
|
33
|
+
case keys.SQL_MODE:
|
|
34
|
+
// value is 8 bytes, but all dcumented flags are in first 4 bytes
|
|
35
|
+
result.sqlMode = buffer.readUInt32LE(offset);
|
|
36
|
+
offset += 8;
|
|
37
|
+
break;
|
|
38
|
+
case keys.CATALOG:
|
|
39
|
+
length = buffer[offset++];
|
|
40
|
+
result.catalog = buffer.toString('utf8', offset, offset + length);
|
|
41
|
+
offset += length + 1; // null byte after string
|
|
42
|
+
break;
|
|
43
|
+
case keys.CHARSET:
|
|
44
|
+
result.clientCharset = buffer.readUInt16LE(offset);
|
|
45
|
+
result.connectionCollation = buffer.readUInt16LE(offset + 2);
|
|
46
|
+
result.serverCharset = buffer.readUInt16LE(offset + 4);
|
|
47
|
+
offset += 6;
|
|
48
|
+
break;
|
|
49
|
+
case keys.TIME_ZONE:
|
|
50
|
+
length = buffer[offset++];
|
|
51
|
+
result.timeZone = buffer.toString('utf8', offset, offset + length);
|
|
52
|
+
offset += length; // no null byte
|
|
53
|
+
break;
|
|
54
|
+
case keys.CATALOG_NZ:
|
|
55
|
+
length = buffer[offset++];
|
|
56
|
+
result.catalogNz = buffer.toString('utf8', offset, offset + length);
|
|
57
|
+
offset += length; // no null byte
|
|
58
|
+
break;
|
|
59
|
+
case keys.LC_TIME_NAMES:
|
|
60
|
+
result.lcTimeNames = buffer.readUInt16LE(offset);
|
|
61
|
+
offset += 2;
|
|
62
|
+
break;
|
|
63
|
+
case keys.CHARSET_DATABASE:
|
|
64
|
+
result.schemaCharset = buffer.readUInt16LE(offset);
|
|
65
|
+
offset += 2;
|
|
66
|
+
break;
|
|
67
|
+
case keys.TABLE_MAP_FOR_UPDATE:
|
|
68
|
+
result.mapForUpdate1 = buffer.readUInt32LE(offset);
|
|
69
|
+
result.mapForUpdate2 = buffer.readUInt32LE(offset + 4);
|
|
70
|
+
offset += 8;
|
|
71
|
+
break;
|
|
72
|
+
case keys.MASTER_DATA_WRITTEN:
|
|
73
|
+
result.masterDataWritten = buffer.readUInt32LE(offset);
|
|
74
|
+
offset += 4;
|
|
75
|
+
break;
|
|
76
|
+
case keys.INVOKERS:
|
|
77
|
+
length = buffer[offset++];
|
|
78
|
+
result.invokerUsername = buffer.toString(
|
|
79
|
+
'utf8',
|
|
80
|
+
offset,
|
|
81
|
+
offset + length
|
|
82
|
+
);
|
|
83
|
+
offset += length;
|
|
84
|
+
length = buffer[offset++];
|
|
85
|
+
result.invokerHostname = buffer.toString(
|
|
86
|
+
'utf8',
|
|
87
|
+
offset,
|
|
88
|
+
offset + length
|
|
89
|
+
);
|
|
90
|
+
offset += length;
|
|
91
|
+
break;
|
|
92
|
+
case keys.UPDATED_DB_NAMES:
|
|
93
|
+
length = buffer[offset++];
|
|
94
|
+
// length - number of null-terminated strings
|
|
95
|
+
result.updatedDBs = []; // we'll store them as array here
|
|
96
|
+
for (; length; --length) {
|
|
97
|
+
prevOffset = offset;
|
|
98
|
+
// fast forward to null terminating byte
|
|
99
|
+
while (buffer[offset++] && offset < buffer.length) {
|
|
100
|
+
// empty body, everything inside while condition
|
|
101
|
+
}
|
|
102
|
+
result.updatedDBs.push(
|
|
103
|
+
buffer.toString('utf8', prevOffset, offset - 1)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
case keys.MICROSECONDS:
|
|
108
|
+
result.microseconds =
|
|
109
|
+
// REVIEW: INVALID UNKNOWN VARIABLE!
|
|
110
|
+
buffer.readInt16LE(offset) + (buffer[offset + 2] << 16);
|
|
111
|
+
offset += 3;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CommandCode = require('../constants/commands.js');
|
|
4
|
+
const ClientConstants = require('../constants/client.js');
|
|
5
|
+
const Packet = require('../packets/packet.js');
|
|
6
|
+
const auth41 = require('../auth_41.js');
|
|
7
|
+
const CharsetToEncoding = require('../constants/charset_encodings.js');
|
|
8
|
+
|
|
9
|
+
// https://dev.mysql.com/doc/internals/en/com-change-user.html#packet-COM_CHANGE_USER
|
|
10
|
+
class ChangeUser {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
this.flags = opts.flags;
|
|
13
|
+
this.user = opts.user || '';
|
|
14
|
+
this.database = opts.database || '';
|
|
15
|
+
this.password = opts.password || '';
|
|
16
|
+
this.passwordSha1 = opts.passwordSha1;
|
|
17
|
+
this.authPluginData1 = opts.authPluginData1;
|
|
18
|
+
this.authPluginData2 = opts.authPluginData2;
|
|
19
|
+
this.connectAttributes = opts.connectAttrinutes || {};
|
|
20
|
+
let authToken;
|
|
21
|
+
if (this.passwordSha1) {
|
|
22
|
+
authToken = auth41.calculateTokenFromPasswordSha(
|
|
23
|
+
this.passwordSha1,
|
|
24
|
+
this.authPluginData1,
|
|
25
|
+
this.authPluginData2
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
authToken = auth41.calculateToken(
|
|
29
|
+
this.password,
|
|
30
|
+
this.authPluginData1,
|
|
31
|
+
this.authPluginData2
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
this.authToken = authToken;
|
|
35
|
+
this.charsetNumber = opts.charsetNumber;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// TODO
|
|
39
|
+
// ChangeUser.fromPacket = function(packet)
|
|
40
|
+
// };
|
|
41
|
+
serializeToBuffer(buffer) {
|
|
42
|
+
const isSet = (flag) => this.flags & ClientConstants[flag];
|
|
43
|
+
const packet = new Packet(0, buffer, 0, buffer.length);
|
|
44
|
+
packet.offset = 4;
|
|
45
|
+
const encoding = CharsetToEncoding[this.charsetNumber];
|
|
46
|
+
packet.writeInt8(CommandCode.CHANGE_USER);
|
|
47
|
+
packet.writeNullTerminatedString(this.user, encoding);
|
|
48
|
+
if (isSet('SECURE_CONNECTION')) {
|
|
49
|
+
packet.writeInt8(this.authToken.length);
|
|
50
|
+
packet.writeBuffer(this.authToken);
|
|
51
|
+
} else {
|
|
52
|
+
packet.writeBuffer(this.authToken);
|
|
53
|
+
packet.writeInt8(0);
|
|
54
|
+
}
|
|
55
|
+
packet.writeNullTerminatedString(this.database, encoding);
|
|
56
|
+
packet.writeInt16(this.charsetNumber);
|
|
57
|
+
if (isSet('PLUGIN_AUTH')) {
|
|
58
|
+
// TODO: read this from parameters
|
|
59
|
+
packet.writeNullTerminatedString('mysql_native_password', 'latin1');
|
|
60
|
+
}
|
|
61
|
+
if (isSet('CONNECT_ATTRS')) {
|
|
62
|
+
const connectAttributes = this.connectAttributes;
|
|
63
|
+
const attrNames = Object.keys(connectAttributes);
|
|
64
|
+
let keysLength = 0;
|
|
65
|
+
for (let k = 0; k < attrNames.length; ++k) {
|
|
66
|
+
keysLength += Packet.lengthCodedStringLength(attrNames[k], encoding);
|
|
67
|
+
keysLength += Packet.lengthCodedStringLength(
|
|
68
|
+
connectAttributes[attrNames[k]],
|
|
69
|
+
encoding
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
packet.writeLengthCodedNumber(keysLength);
|
|
73
|
+
for (let k = 0; k < attrNames.length; ++k) {
|
|
74
|
+
packet.writeLengthCodedString(attrNames[k], encoding);
|
|
75
|
+
packet.writeLengthCodedString(
|
|
76
|
+
connectAttributes[attrNames[k]],
|
|
77
|
+
encoding
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return packet;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toPacket() {
|
|
85
|
+
if (typeof this.user !== 'string') {
|
|
86
|
+
throw new Error('"user" connection config property must be a string');
|
|
87
|
+
}
|
|
88
|
+
if (typeof this.database !== 'string') {
|
|
89
|
+
throw new Error('"database" connection config property must be a string');
|
|
90
|
+
}
|
|
91
|
+
// dry run: calculate resulting packet length
|
|
92
|
+
const p = this.serializeToBuffer(Packet.MockBuffer());
|
|
93
|
+
return this.serializeToBuffer(Buffer.allocUnsafe(p.offset));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = ChangeUser;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Packet = require('../packets/packet');
|
|
4
|
+
const CommandCodes = require('../constants/commands');
|
|
5
|
+
|
|
6
|
+
class CloseStatement {
|
|
7
|
+
constructor(id) {
|
|
8
|
+
this.id = id;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// note: no response sent back
|
|
12
|
+
toPacket() {
|
|
13
|
+
const packet = new Packet(0, Buffer.allocUnsafe(9), 0, 9);
|
|
14
|
+
packet.offset = 4;
|
|
15
|
+
packet.writeInt8(CommandCodes.STMT_CLOSE);
|
|
16
|
+
packet.writeInt32(this.id);
|
|
17
|
+
return packet;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = CloseStatement;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Packet = require('../packets/packet');
|
|
4
|
+
const StringParser = require('../parsers/string');
|
|
5
|
+
const CharsetToEncoding = require('../constants/charset_encodings.js');
|
|
6
|
+
|
|
7
|
+
const fields = ['catalog', 'schema', 'table', 'orgTable', 'name', 'orgName'];
|
|
8
|
+
|
|
9
|
+
// creating JS string is relatively expensive (compared to
|
|
10
|
+
// reading few bytes from buffer) because all string properties
|
|
11
|
+
// except for name are unlikely to be used we postpone
|
|
12
|
+
// string conversion until property access
|
|
13
|
+
//
|
|
14
|
+
// TODO: watch for integration benchmarks (one with real network buffer)
|
|
15
|
+
// there could be bad side effect as keeping reference to a buffer makes it
|
|
16
|
+
// sit in the memory longer (usually until final .query() callback)
|
|
17
|
+
// Latest v8 perform much better in regard to bufferer -> string conversion,
|
|
18
|
+
// at some point of time this optimisation might become unnecessary
|
|
19
|
+
// see https://github.com/sidorares/node-mysql2/pull/137
|
|
20
|
+
//
|
|
21
|
+
class ColumnDefinition {
|
|
22
|
+
constructor(packet, clientEncoding) {
|
|
23
|
+
this._buf = packet.buffer;
|
|
24
|
+
this._clientEncoding = clientEncoding;
|
|
25
|
+
this._catalogLength = packet.readLengthCodedNumber();
|
|
26
|
+
this._catalogStart = packet.offset;
|
|
27
|
+
packet.offset += this._catalogLength;
|
|
28
|
+
this._schemaLength = packet.readLengthCodedNumber();
|
|
29
|
+
this._schemaStart = packet.offset;
|
|
30
|
+
packet.offset += this._schemaLength;
|
|
31
|
+
this._tableLength = packet.readLengthCodedNumber();
|
|
32
|
+
this._tableStart = packet.offset;
|
|
33
|
+
packet.offset += this._tableLength;
|
|
34
|
+
this._orgTableLength = packet.readLengthCodedNumber();
|
|
35
|
+
this._orgTableStart = packet.offset;
|
|
36
|
+
packet.offset += this._orgTableLength;
|
|
37
|
+
// name is always used, don't make it lazy
|
|
38
|
+
const _nameLength = packet.readLengthCodedNumber();
|
|
39
|
+
const _nameStart = packet.offset;
|
|
40
|
+
packet.offset += _nameLength;
|
|
41
|
+
this._orgNameLength = packet.readLengthCodedNumber();
|
|
42
|
+
this._orgNameStart = packet.offset;
|
|
43
|
+
packet.offset += this._orgNameLength;
|
|
44
|
+
packet.skip(1); // length of the following fields (always 0x0c)
|
|
45
|
+
this.characterSet = packet.readInt16();
|
|
46
|
+
this.encoding = CharsetToEncoding[this.characterSet];
|
|
47
|
+
this.name = StringParser.decode(
|
|
48
|
+
this._buf,
|
|
49
|
+
this.encoding === 'binary' ? this._clientEncoding : this.encoding,
|
|
50
|
+
_nameStart,
|
|
51
|
+
_nameStart + _nameLength
|
|
52
|
+
);
|
|
53
|
+
this.columnLength = packet.readInt32();
|
|
54
|
+
this.columnType = packet.readInt8();
|
|
55
|
+
this.type = this.columnType;
|
|
56
|
+
this.flags = packet.readInt16();
|
|
57
|
+
this.decimals = packet.readInt8();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
inspect() {
|
|
61
|
+
return {
|
|
62
|
+
catalog: this.catalog,
|
|
63
|
+
schema: this.schema,
|
|
64
|
+
name: this.name,
|
|
65
|
+
orgName: this.orgName,
|
|
66
|
+
table: this.table,
|
|
67
|
+
orgTable: this.orgTable,
|
|
68
|
+
characterSet: this.characterSet,
|
|
69
|
+
encoding: this.encoding,
|
|
70
|
+
columnLength: this.columnLength,
|
|
71
|
+
type: this.columnType,
|
|
72
|
+
flags: this.flags,
|
|
73
|
+
decimals: this.decimals,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth, inspectOptions, inspect) {
|
|
78
|
+
const Types = require('../constants/types.js');
|
|
79
|
+
const typeNames = [];
|
|
80
|
+
for (const t in Types) {
|
|
81
|
+
typeNames[Types[t]] = t;
|
|
82
|
+
}
|
|
83
|
+
const fiedFlags = require('../constants/field_flags.js');
|
|
84
|
+
const flagNames = [];
|
|
85
|
+
// TODO: respect inspectOptions.showHidden
|
|
86
|
+
//const inspectFlags = inspectOptions.showHidden ? this.flags : this.flags & ~fiedFlags.PRI_KEY;
|
|
87
|
+
const inspectFlags = this.flags;
|
|
88
|
+
for (const f in fiedFlags) {
|
|
89
|
+
if (inspectFlags & fiedFlags[f]) {
|
|
90
|
+
if (f === 'PRI_KEY') {
|
|
91
|
+
flagNames.push('PRIMARY KEY');
|
|
92
|
+
} else if (f === 'NOT_NULL') {
|
|
93
|
+
flagNames.push('NOT NULL');
|
|
94
|
+
} else if (f === 'BINARY') {
|
|
95
|
+
// ignore flag for now
|
|
96
|
+
} else if (f === 'MULTIPLE_KEY') {
|
|
97
|
+
// not sure if that should be part of inspection.
|
|
98
|
+
// in the schema usually this is part of index definition
|
|
99
|
+
// example: UNIQUE KEY `my_uniq_id` (`id_box_elements`,`id_router`)
|
|
100
|
+
// note that only first column has MULTIPLE_KEY flag set in this case
|
|
101
|
+
// so there is no good way of knowing that this is part of index just
|
|
102
|
+
// by looking at indifidual field flags
|
|
103
|
+
} else if (f === 'NO_DEFAULT_VALUE') {
|
|
104
|
+
// almost the same as NOT_NULL?
|
|
105
|
+
} else if (f === 'BLOB') {
|
|
106
|
+
// included in the type
|
|
107
|
+
} else if (f === 'UNSIGNED') {
|
|
108
|
+
// this should be first after type
|
|
109
|
+
} else if (f === 'TIMESTAMP') {
|
|
110
|
+
// timestamp flag is redundant for inspection - already included in type
|
|
111
|
+
} else if (f === 'ON_UPDATE_NOW') {
|
|
112
|
+
flagNames.push('ON UPDATE CURRENT_TIMESTAMP');
|
|
113
|
+
} else {
|
|
114
|
+
flagNames.push(f);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (depth > 1) {
|
|
120
|
+
return inspect({
|
|
121
|
+
...this.inspect(),
|
|
122
|
+
typeName: typeNames[this.columnType],
|
|
123
|
+
flags: flagNames,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const isUnsigned = this.flags & fiedFlags.UNSIGNED;
|
|
128
|
+
|
|
129
|
+
let typeName = typeNames[this.columnType];
|
|
130
|
+
if (typeName === 'BLOB') {
|
|
131
|
+
// TODO: check for non-utf8mb4 encoding
|
|
132
|
+
if (this.columnLength === 4294967295) {
|
|
133
|
+
typeName = 'LONGTEXT';
|
|
134
|
+
} else if (this.columnLength === 67108860) {
|
|
135
|
+
typeName = 'MEDIUMTEXT';
|
|
136
|
+
} else if (this.columnLength === 262140) {
|
|
137
|
+
typeName = 'TEXT';
|
|
138
|
+
} else if (this.columnLength === 1020) {
|
|
139
|
+
// 255*4
|
|
140
|
+
typeName = 'TINYTEXT';
|
|
141
|
+
} else {
|
|
142
|
+
typeName = `BLOB(${this.columnLength})`;
|
|
143
|
+
}
|
|
144
|
+
} else if (typeName === 'VAR_STRING') {
|
|
145
|
+
// TODO: check for non-utf8mb4 encoding
|
|
146
|
+
typeName = `VARCHAR(${Math.ceil(this.columnLength / 4)})`;
|
|
147
|
+
} else if (typeName === 'TINY') {
|
|
148
|
+
if (
|
|
149
|
+
(this.columnLength === 3 && isUnsigned) ||
|
|
150
|
+
(this.columnLength === 4 && !isUnsigned)
|
|
151
|
+
) {
|
|
152
|
+
typeName = 'TINYINT';
|
|
153
|
+
} else {
|
|
154
|
+
typeName = `TINYINT(${this.columnLength})`;
|
|
155
|
+
}
|
|
156
|
+
} else if (typeName === 'LONGLONG') {
|
|
157
|
+
if (this.columnLength === 20) {
|
|
158
|
+
typeName = 'BIGINT';
|
|
159
|
+
} else {
|
|
160
|
+
typeName = `BIGINT(${this.columnLength})`;
|
|
161
|
+
}
|
|
162
|
+
} else if (typeName === 'SHORT') {
|
|
163
|
+
if (isUnsigned && this.columnLength === 5) {
|
|
164
|
+
typeName = 'SMALLINT';
|
|
165
|
+
} else if (!isUnsigned && this.columnLength === 6) {
|
|
166
|
+
typeName = 'SMALLINT';
|
|
167
|
+
} else {
|
|
168
|
+
typeName = `SMALLINT(${this.columnLength})`;
|
|
169
|
+
}
|
|
170
|
+
} else if (typeName === 'LONG') {
|
|
171
|
+
if (isUnsigned && this.columnLength === 10) {
|
|
172
|
+
typeName = 'INT';
|
|
173
|
+
} else if (!isUnsigned && this.columnLength === 11) {
|
|
174
|
+
typeName = 'INT';
|
|
175
|
+
} else {
|
|
176
|
+
typeName = `INT(${this.columnLength})`;
|
|
177
|
+
}
|
|
178
|
+
} else if (typeName === 'INT24') {
|
|
179
|
+
if (isUnsigned && this.columnLength === 8) {
|
|
180
|
+
typeName = 'MEDIUMINT';
|
|
181
|
+
} else if (!isUnsigned && this.columnLength === 9) {
|
|
182
|
+
typeName = 'MEDIUMINT';
|
|
183
|
+
} else {
|
|
184
|
+
typeName = `MEDIUMINT(${this.columnLength})`;
|
|
185
|
+
}
|
|
186
|
+
} else if (typeName === 'DOUBLE') {
|
|
187
|
+
// DOUBLE without modifiers is reported as DOUBLE(22, 31)
|
|
188
|
+
if (this.columnLength === 22 && this.decimals === 31) {
|
|
189
|
+
typeName = 'DOUBLE';
|
|
190
|
+
} else {
|
|
191
|
+
typeName = `DOUBLE(${this.columnLength},${this.decimals})`;
|
|
192
|
+
}
|
|
193
|
+
} else if (typeName === 'FLOAT') {
|
|
194
|
+
// FLOAT without modifiers is reported as FLOAT(12, 31)
|
|
195
|
+
if (this.columnLength === 12 && this.decimals === 31) {
|
|
196
|
+
typeName = 'FLOAT';
|
|
197
|
+
} else {
|
|
198
|
+
typeName = `FLOAT(${this.columnLength},${this.decimals})`;
|
|
199
|
+
}
|
|
200
|
+
} else if (typeName === 'NEWDECIMAL') {
|
|
201
|
+
if (this.columnLength === 11 && this.decimals === 0) {
|
|
202
|
+
typeName = 'DECIMAL';
|
|
203
|
+
} else if (this.decimals === 0) {
|
|
204
|
+
// not sure why, but DECIMAL(13) is reported as DECIMAL(14, 0)
|
|
205
|
+
// and DECIMAL(13, 9) is reported as NEWDECIMAL(15, 9)
|
|
206
|
+
if (isUnsigned) {
|
|
207
|
+
typeName = `DECIMAL(${this.columnLength})`;
|
|
208
|
+
} else {
|
|
209
|
+
typeName = `DECIMAL(${this.columnLength - 1})`;
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
typeName = `DECIMAL(${this.columnLength - 2},${this.decimals})`;
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
typeName = `${typeNames[this.columnType]}(${this.columnLength})`;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (isUnsigned) {
|
|
219
|
+
typeName += ' UNSIGNED';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// TODO respect colors option
|
|
223
|
+
return `\`${this.name}\` ${[typeName, ...flagNames].join(' ')}`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static toPacket(column, sequenceId) {
|
|
227
|
+
let length = 17; // = 4 padding + 1 + 12 for the rest
|
|
228
|
+
fields.forEach((field) => {
|
|
229
|
+
length += Packet.lengthCodedStringLength(
|
|
230
|
+
column[field],
|
|
231
|
+
CharsetToEncoding[column.characterSet]
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
235
|
+
|
|
236
|
+
const packet = new Packet(sequenceId, buffer, 0, length);
|
|
237
|
+
function writeField(name) {
|
|
238
|
+
packet.writeLengthCodedString(
|
|
239
|
+
column[name],
|
|
240
|
+
CharsetToEncoding[column.characterSet]
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
packet.offset = 4;
|
|
244
|
+
fields.forEach(writeField);
|
|
245
|
+
packet.writeInt8(0x0c);
|
|
246
|
+
packet.writeInt16(column.characterSet);
|
|
247
|
+
packet.writeInt32(column.columnLength);
|
|
248
|
+
packet.writeInt8(column.columnType);
|
|
249
|
+
packet.writeInt16(column.flags);
|
|
250
|
+
packet.writeInt8(column.decimals);
|
|
251
|
+
packet.writeInt16(0); // filler
|
|
252
|
+
return packet;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// node-mysql compatibility: alias "db" to "schema"
|
|
256
|
+
get db() {
|
|
257
|
+
return this.schema;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const addString = function (name) {
|
|
262
|
+
Object.defineProperty(ColumnDefinition.prototype, name, {
|
|
263
|
+
get: function () {
|
|
264
|
+
const start = this[`_${name}Start`];
|
|
265
|
+
const end = start + this[`_${name}Length`];
|
|
266
|
+
const val = StringParser.decode(
|
|
267
|
+
this._buf,
|
|
268
|
+
this.encoding === 'binary' ? this._clientEncoding : this.encoding,
|
|
269
|
+
start,
|
|
270
|
+
end
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
Object.defineProperty(this, name, {
|
|
274
|
+
value: val,
|
|
275
|
+
writable: false,
|
|
276
|
+
configurable: false,
|
|
277
|
+
enumerable: false,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return val;
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
addString('catalog');
|
|
286
|
+
addString('schema');
|
|
287
|
+
addString('table');
|
|
288
|
+
addString('orgTable');
|
|
289
|
+
addString('orgName');
|
|
290
|
+
|
|
291
|
+
module.exports = ColumnDefinition;
|