2014-09-01 17:18:45 +02:00
|
|
|
|
2014-06-09 16:27:03 -04:00
|
|
|
// MySQL2 Client
|
|
|
|
// -------
|
2015-05-09 14:01:19 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var inherits = require('inherits');
|
|
|
|
var Client_MySQL = require('../mysql');
|
|
|
|
var Promise = require('../../promise');
|
|
|
|
var helpers = require('../../helpers');
|
|
|
|
var pick = require('lodash/object/pick');
|
|
|
|
var pluck = require('lodash/collection/pluck');
|
|
|
|
var assign = require('lodash/object/assign');
|
|
|
|
var Transaction = require('./transaction');
|
2014-09-03 10:48:25 +02:00
|
|
|
|
2014-12-02 16:10:44 -08:00
|
|
|
var configOptions = ['user', 'database', 'host', 'password', 'port', 'ssl', 'connection', 'stream'];
|
2014-06-09 16:27:03 -04:00
|
|
|
|
|
|
|
// Always initialize with the "QueryBuilder" and "QueryCompiler"
|
|
|
|
// objects, which extend the base 'lib/query/builder' and
|
|
|
|
// 'lib/query/compiler', respectively.
|
2015-04-17 15:00:08 -04:00
|
|
|
function Client_MySQL2(config) {
|
2015-05-09 13:58:18 -04:00
|
|
|
Client_MySQL.call(this, config);
|
2014-06-09 16:27:03 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
inherits(Client_MySQL2, Client_MySQL);
|
2014-06-09 16:27:03 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
assign(Client_MySQL2.prototype, {
|
|
|
|
|
|
|
|
// The "dialect", for reference elsewhere.
|
|
|
|
driverName: 'mysql2',
|
|
|
|
|
2015-05-01 11:52:48 -04:00
|
|
|
Transaction: Transaction,
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
_driver: function _driver() {
|
|
|
|
return require('mysql2');
|
|
|
|
},
|
2015-04-29 17:11:52 -04:00
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2015-05-09 13:58:18 -04:00
|
|
|
acquireRawConnection: function acquireRawConnection() {
|
|
|
|
var connection = this.driver.createConnection(pick(this.connectionSettings, configOptions));
|
|
|
|
return new Promise(function (resolver, rejecter) {
|
|
|
|
connection.connect(function (err) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
resolver(connection);
|
|
|
|
});
|
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
processResponse: function processResponse(obj, runner) {
|
|
|
|
var response = obj.response;
|
|
|
|
var method = obj.method;
|
|
|
|
var rows = response[0];
|
|
|
|
var fields = response[1];
|
|
|
|
if (obj.output) return obj.output.call(runner, rows, fields);
|
2015-04-22 10:34:14 -04:00
|
|
|
switch (method) {
|
|
|
|
case 'select':
|
|
|
|
case 'pluck':
|
|
|
|
case 'first':
|
2015-05-09 13:58:18 -04:00
|
|
|
var resp = helpers.skim(rows);
|
|
|
|
if (method === 'pluck') return pluck(resp, obj.pluck);
|
|
|
|
return method === 'first' ? resp[0] : resp;
|
2015-04-22 10:34:14 -04:00
|
|
|
case 'insert':
|
2015-05-09 13:58:18 -04:00
|
|
|
return [rows.insertId];
|
2015-04-22 10:34:14 -04:00
|
|
|
case 'del':
|
|
|
|
case 'update':
|
|
|
|
case 'counter':
|
2015-05-09 13:58:18 -04:00
|
|
|
return rows.affectedRows;
|
2015-04-22 10:34:14 -04:00
|
|
|
default:
|
2015-05-09 13:58:18 -04:00
|
|
|
return response;
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
module.exports = Client_MySQL2;
|