2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// MySQL2 Client
|
|
|
|
// -------
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import Client_MySQL from '../mysql';
|
2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2016-05-17 01:01:34 +10:00
|
|
|
import * as helpers from '../../helpers';
|
2016-05-18 20:22:50 +10:00
|
|
|
import { pick, map, assign } from 'lodash'
|
2016-05-17 01:01:34 +10:00
|
|
|
import Transaction from './transaction';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
const configOptions = [
|
2015-09-16 19:54:01 -07:00
|
|
|
'isServer',
|
|
|
|
'stream',
|
|
|
|
'host',
|
|
|
|
'port',
|
|
|
|
'localAddress',
|
|
|
|
'socketPath',
|
|
|
|
'user',
|
|
|
|
'password',
|
|
|
|
'passwordSha1',
|
|
|
|
'database',
|
|
|
|
'connectTimeout',
|
|
|
|
'insecureAuth',
|
|
|
|
'supportBigNumbers',
|
|
|
|
'bigNumberStrings',
|
|
|
|
'decimalNumbers',
|
|
|
|
'dateStrings',
|
|
|
|
'debug',
|
|
|
|
'trace',
|
|
|
|
'stringifyObjects',
|
|
|
|
'timezone',
|
|
|
|
'flags',
|
|
|
|
'queryFormat',
|
|
|
|
'pool',
|
|
|
|
'ssl',
|
|
|
|
'multipleStatements',
|
|
|
|
'namedPlaceholders',
|
|
|
|
'typeCast',
|
|
|
|
'charsetNumber',
|
|
|
|
'compress'
|
|
|
|
];
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
|
|
|
// objects, which extend the base 'lib/query/builder' and
|
|
|
|
// 'lib/query/compiler', respectively.
|
|
|
|
function Client_MySQL2(config) {
|
|
|
|
Client_MySQL.call(this, config)
|
|
|
|
}
|
|
|
|
inherits(Client_MySQL2, Client_MySQL)
|
|
|
|
|
|
|
|
assign(Client_MySQL2.prototype, {
|
|
|
|
|
|
|
|
// The "dialect", for reference elsewhere.
|
|
|
|
driverName: 'mysql2',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
Transaction,
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_driver() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return require('mysql2')
|
2015-07-02 18:28:34 -04:00
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireRawConnection() {
|
|
|
|
const client = this;
|
|
|
|
const connection = this.driver.createConnection(pick(this.connectionSettings, configOptions))
|
2015-05-09 13:58:18 -04:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
connection.connect(function(err) {
|
|
|
|
if (err) return rejecter(err)
|
2016-03-29 17:46:19 +02:00
|
|
|
connection.on('error', client._connectionErrorHandler.bind(null, client, connection))
|
2015-05-09 13:58:18 -04:00
|
|
|
resolver(connection)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
processResponse(obj, runner) {
|
|
|
|
const { response } = obj
|
2016-05-18 19:59:24 +10:00
|
|
|
const { method } = obj
|
|
|
|
const rows = response[0]
|
|
|
|
const fields = response[1]
|
2015-05-09 13:58:18 -04:00
|
|
|
if (obj.output) return obj.output.call(runner, rows, fields)
|
|
|
|
switch (method) {
|
|
|
|
case 'select':
|
|
|
|
case 'pluck':
|
2016-05-17 01:01:34 +10:00
|
|
|
case 'first': {
|
|
|
|
const resp = helpers.skim(rows)
|
2016-03-02 16:52:32 +01:00
|
|
|
if (method === 'pluck') return map(resp, obj.pluck)
|
2015-05-09 13:58:18 -04:00
|
|
|
return method === 'first' ? resp[0] : resp
|
2016-05-17 01:01:34 +10:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
case 'insert':
|
|
|
|
return [rows.insertId]
|
|
|
|
case 'del':
|
|
|
|
case 'update':
|
|
|
|
case 'counter':
|
|
|
|
return rows.affectedRows
|
|
|
|
default:
|
|
|
|
return response
|
|
|
|
}
|
2016-03-19 19:14:46 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ping(resource, callback) {
|
2016-03-19 19:14:46 +01:00
|
|
|
resource.query('SELECT 1', callback);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default Client_MySQL2;
|