Skip to content

Commit

Permalink
chore: use arrows stage 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchick committed Nov 15, 2018
1 parent 1ae3e89 commit e55abc7
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 214 deletions.
2 changes: 1 addition & 1 deletion documentation/Authentication-Switch.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const conn = mysql.createConnection({
database: 'test_database',
authSwitchHandler: function ({pluginName, pluginData}, cb) {
if (pluginName === 'ssh-key-auth') {
getPrivateKey(function (key) {
getPrivateKey(key => {
const response = encrypt(key, pluginData);
// continue handshake by sending response data
// respond with error to propagate error to connect/changeUser handlers
Expand Down
12 changes: 6 additions & 6 deletions documentation/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const mysql = require('mysql2');
const connection = mysql.createConnection({user: 'test', database: 'test'});

connection.query('SELECT 1+1 as test1', function (err, rows) {
connection.query('SELECT 1+1 as test1', (err, rows) => {
//
});
```
Expand All @@ -17,7 +17,7 @@ connection.query('SELECT 1+1 as test1', function (err, rows) {
const mysql = require('mysql2');
const connection = mysql.createConnection({user: 'test', database: 'test'});

connection.execute('SELECT 1+? as test1', [10], function (err, rows) {
connection.execute('SELECT 1+? as test1', [10], (err, rows) => {
//
});
```
Expand Down Expand Up @@ -49,7 +49,7 @@ const connection = mysql.createConnection({
ssl: 'Amazon RDS'
});

conn.query('show status like \'Ssl_cipher\'', function (err, res) {
conn.query('show status like \'Ssl_cipher\'', (err, res) => {
console.log(err, res);
conn.end();
});
Expand All @@ -63,7 +63,7 @@ const mysql = require('mysql2');

const server = mysql.createServer();
server.listen(3307);
server.on('connection', function (conn) {
server.on('connection', conn => {
console.log('connection');

conn.serverHandshake({
Expand All @@ -75,14 +75,14 @@ server.on('connection', function (conn) {
capabilityFlags: 0xffffff
});

conn.on('field_list', function (table, fields) {
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', function (sql) {
conn.on('query', sql => {
console.log('proxying query:' + sql);
remote.query(sql, function (err) {
// overloaded args, either (err, result :object)
Expand Down
8 changes: 4 additions & 4 deletions documentation/Extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ You can use named placeholders for parameters by setting `namedPlaceholders` con

```js
connection.config.namedPlaceholders = true;
connection.execute('select :x + :y as z', {x: 1, y: 2}, function (err, rows) {
connection.execute('select :x + :y as z', {x: 1, y: 2}, (err, rows) => {
// statement prepared as "select ? + ? as z" and executed with [1,2] values
// rows returned: [ { z: 3 } ]
});

connection.execute('select :x + :x as z', {x: 1}, function (err, rows) {
connection.execute('select :x + :x as z', {x: 1}, (err, rows) => {
// select ? + ? as z, execute with [1, 1]
});

connection.query('select :x + :x as z', {x: 1}, function (err, rows) {
connection.query('select :x + :x as z', {x: 1}, (err, rows) => {
// query select 1 + 1 as z
});
```
Expand All @@ -24,7 +24,7 @@ You can use named placeholders for parameters by setting `namedPlaceholders` con

```js
const options = {sql: 'select A,B,C,D from foo', rowsAsArray: true};
connection.query(options, function (err, results) {
connection.query(options, (err, results) => {
/* results will be an array of arrays like this now:
[[
'field A value',
Expand Down
6 changes: 3 additions & 3 deletions documentation/Prepared-Statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Similar to `connection.query()`.

```js
connection.execute('select 1 + ? + ? as result', [5, 6], function (err, rows) {
connection.execute('select 1 + ? + ? as result', [5, 6], (err, rows) => {
// rows: [ { result: 12 } ]
// internally 'select 1 + ? + ? as result' is prepared first. On subsequent calls cached statement is re-used
});
Expand All @@ -17,13 +17,13 @@ connection.unprepare('select 1 + ? + ? as result');
## Manual prepare / execute

```js
connection.prepare('select ? + ? as tests', function (err, statement) {
connection.prepare('select ? + ? as tests', (err, statement) => {
// statement.parameters - array of column definitions, length === number of params, here 2
// statement.columns - array of result column definitions. Can be empty if result schema is dynamic / not known
// statement.id
// statement.query

statement.execute([1, 2], function (err, rows, columns) {
statement.execute([1, 2], (err, rows, columns) => {
// -> [ { tests: 3 } ]
});

Expand Down
8 changes: 4 additions & 4 deletions documentation/Promise-Wrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ In addition to errback interface there is thin wrapper to expose Promise-based a
/* eslint-env es6 */
const mysql = require('mysql2/promise'); // or require('mysql2').createConnectionPromise
mysql.createConnection({ /* same parameters as for non-promise createConnection */ })
.then((conn) => conn.query('select foo from bar'))
.then(conn => conn.query('select foo from bar'))
.then(([rows, fields]) => console.log(rows[0].foo));
```

```js
const pool = require('mysql2/promise').createPool({}); // or mysql.createPoolPromise({})
pool.getConnection()
.then((conn) => {
.then(conn => {
const res = conn.query('select foo from bar');
conn.release();
return res;
}).then((result) => {
}).then(result => {
console.log(result[0][0].foo);
}).catch((err) => {
}).catch(err => {
console.log(err); // any of connection time or query time errors from above
});
```
Expand Down
2 changes: 1 addition & 1 deletion examples/binlog-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const binlogStream = mysql.createBinlogStream({
});

binlogStream.pipe(
through2.obj(function(obj, enc, next) {
through2.obj((obj, enc, next) => {
console.log(obj);
next();
})
Expand Down
6 changes: 3 additions & 3 deletions examples/connect-over-socks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const conn1 = mysql.createPool({
}
});

conn1.execute('select sleep(1.1) as www', function(err, rows, fields) {
conn1.execute('select sleep(1.1) as www', (err, rows, fields) => {
console.log(err, rows, fields);
});

conn1.execute('select sleep(1) as qqq', function(err, rows, fields) {
conn1.execute('select sleep(1) as qqq', (err, rows, fields) => {
console.log(err, rows, fields);
});

conn1.execute('select sleep(1) as qqq', function(err, rows, fields) {
conn1.execute('select sleep(1) as qqq', (err, rows, fields) => {
console.log(err, rows, fields);
});
6 changes: 3 additions & 3 deletions examples/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const connection = mysql.createConnection({
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[1, null, 3],
function(err, rows, fields) {
(err, rows, fields) => {
console.log(err, rows, fields);
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[3, null, 3],
function(err, rows, fields) {
(err, rows, fields) => {
console.log(err, rows, fields);
connection.unprepare('select ?+1 as qqq, ? as rrr, ? as yyy');
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[3, null, 3],
function(err, rows, fields) {
(err, rows, fields) => {
console.log(err, rows, fields);
}
);
Expand Down
6 changes: 3 additions & 3 deletions examples/mysqlproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ClientFlags = require('mysql2/lib/constants/client.js');
const server = mysql.createServer();
server.listen(3307);

server.on('connection', function(conn) {
server.on('connection', conn => {
console.log('connection');

conn.serverHandshake({
Expand All @@ -18,7 +18,7 @@ server.on('connection', function(conn) {
capabilityFlags: 0xffffff ^ ClientFlags.COMPRESS
});

conn.on('field_list', function(table, fields) {
conn.on('field_list', (table, fields) => {
console.log('field list:', table, fields);
conn.writeEof();
});
Expand All @@ -30,7 +30,7 @@ server.on('connection', function(conn) {
password: 'secret'
});

conn.on('query', function(sql) {
conn.on('query', sql => {
console.log('proxying query:' + sql);
remote.query(sql, function(err) {
// overloaded args, either (err, result :object)
Expand Down
14 changes: 7 additions & 7 deletions examples/pass-sha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const mysql = require('mysql2').createConnection({
passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex')
});

mysql.execute('select ?+1 as qqq, ? as rrr, ? as yyy', [1, null, 3], function(
err,
rows,
fields
) {
console.log(err, rows, fields);
});
mysql.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[1, null, 3],
(err, rows, fields) => {
console.log(err, rows, fields);
}
);
10 changes: 5 additions & 5 deletions examples/pool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const pool = require('mysql2').createPool({
password: 'root'
});

setInterval(function() {
setInterval(() => {
for (let i = 0; i < 5; ++i) {
pool.query(function(err, db) {
pool.query((err, db) => {
console.log(rows, fields);
// Connection is automatically released once query resolves
});
}
}, 1000);

setInterval(function() {
setInterval(() => {
for (let i = 0; i < 5; ++i) {
pool.getConnection(function(err, db) {
db.query('select sleep(0.5) as qqq', function(err, rows, fields) {
pool.getConnection((err, db) => {
db.query('select sleep(0.5) as qqq', (err, rows, fields) => {
console.log(rows, fields);
db.release();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const connection = mysql.createConnection({
connection.execute(
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
['Rick C-137', 53],
function(err, results, fields) {
(err, results, fields) => {
console.log(results); // results contains rows returned by server
console.log(fields); // fields contains extra meta data about results, if available

Expand Down
6 changes: 3 additions & 3 deletions examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function authenticate(params, cb) {

const server = mysql.createServer();
server.listen(3333);
server.on('connection', function(conn) {
server.on('connection', conn => {
// we can deny connection here:
// conn.writeError({ message: 'secret', code: 123 });
// conn.close();
Expand All @@ -39,12 +39,12 @@ server.on('connection', function(conn) {
authCallback: authenticate
});

conn.on('field_list', function(table, fields) {
conn.on('field_list', (table, fields) => {
console.log('FIELD LIST:', table, fields);
conn.writeEof();
});

conn.on('query', function(query) {
conn.on('query', query => {
conn.writeColumns([
{
catalog: 'def',
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const connection = mysql.createConnection({
// simple query
connection.query(
'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
function(err, results, fields) {
(err, results, fields) => {
console.log(results); // results contains rows returned by server
console.log(fields); // fields contains extra meta data about results, if available
}
Expand All @@ -23,7 +23,7 @@ connection.query(
connection.query(
'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
['Page', 45],
function(err, results) {
(err, results) => {
console.log(results);
}
);
7 changes: 3 additions & 4 deletions lib/commands/client_handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class ClientHandshake extends Command {
}

handshakeInit(helloPacket, connection) {
const command = this;
this.on('error', e => {
connection._fatalError = e;
connection._protocolError = e;
Expand Down Expand Up @@ -115,7 +114,7 @@ class ClientHandshake extends Command {
const err = new Error('Server does not support secure connnection');
err.code = 'HANDSHAKE_NO_SSL_SUPPORT';
err.fatal = true;
command.emit('error', err);
this.emit('error', err);
return false;
}
// send ssl upgrade request and immediately upgrade connection to secure
Expand All @@ -127,11 +126,11 @@ class ClientHandshake extends Command {
// SSL negotiation error are fatal
err.code = 'HANDSHAKE_SSL_ERROR';
err.fatal = true;
command.emit('error', err);
this.emit('error', err);
return;
}
// rest of communication is encrypted
command.sendCredentials(connection);
this.sendCredentials(connection);
});
} else {
this.sendCredentials(connection);
Expand Down
11 changes: 5 additions & 6 deletions lib/commands/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,16 @@ class Prepare extends Command {
}

prepareDone(connection) {
const self = this;
const statement = new PreparedStatementInfo(
self.query,
self.id,
self.fields,
self.parameterDefinitions,
this.query,
this.id,
this.fields,
this.parameterDefinitions,
connection
);
connection._statements.set(this.key, statement);
if (this.onResult) {
self.onResult(null, statement);
this.onResult(null, statement);
}
return null;
}
Expand Down
Loading

0 comments on commit e55abc7

Please sign in to comment.