forked from sidorares/node-mysql2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlproxy.js
55 lines (47 loc) · 1.42 KB
/
mysqlproxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const mysql = require('mysql2');
const ClientFlags = require('mysql2/lib/constants/client.js');
const server = mysql.createServer();
server.listen(3307);
server.on('connection', conn => {
console.log('connection');
conn.serverHandshake({
protocolVersion: 10,
serverVersion: 'node.js rocks',
connectionId: 1234,
statusFlags: 2,
characterSet: 8,
capabilityFlags: 0xffffff ^ ClientFlags.COMPRESS
});
conn.on('field_list', (table, fields) => {
console.log('field list:', table, fields);
conn.writeEof();
});
const remote = mysql.createConnection({
user: 'root',
database: 'dbname',
host: 'server.example.com',
password: 'secret'
});
conn.on('query', sql => {
console.log(`proxying query: ${sql}`);
remote.query(sql, function(err) {
// overloaded args, either (err, result :object)
// or (err, rows :array, columns :array)
if (Array.isArray(arguments[1])) {
// response to a 'select', 'show' or similar
const rows = arguments[1],
columns = arguments[2];
console.log('rows', rows);
console.log('columns', columns);
conn.writeTextResult(rows, columns);
} else {
// response to an 'insert', 'update' or 'delete'
const result = arguments[1];
console.log('result', result);
conn.writeOk(result);
}
});
});
conn.on('end', remote.end.bind(remote));
});