lib/adapters/mysql/mysql.js
'use strict';
var _ = require('lodash');
var Promise = require('bluebird');
var Adapter = require('../base');
var mysql = require('mysql');
Promise.promisifyAll(mysql);
Promise.promisifyAll(require('mysql/lib/Connection').prototype);
var returning = require('../mixins/returning');
var EmbedPseudoReturn = returning.EmbedPseudoReturn;
var ExtractPseudoReturn = returning.ExtractPseudoReturn;
/**
* MySQL Adapter.
*
* @public
* @constructor
* @extends Adapter
*/
var MySQLAdapter = Adapter.extend(/** @lends MySQLAdapter# */ {
/**
* Connect for MySQLAdapter.
*
* @method
* @protected
* @see {@link Adapter#_connect}
*/
_connect: Promise.method(function() {
return mysql.createConnection(this._connection);
}),
/**
* Disconnect for MySQLAdapter.
*
* @method
* @protected
* @see {@link Adapter#_disconnect}
*/
_disconnect: Promise.method(function(connection) {
return connection.end();
}),
/**
* Execute for MySQLAdapter.
*
* @method
* @private
* @see {@link Adapter#_execute}
*/
_execute: Promise.method(function(connection, sql, args, returning) {
return Promise.bind({})
.then(function() {
return connection.queryAsync(sql, args);
})
.spread(function(rows, fields) {
if (rows.insertId) { returning(rows.insertId); }
return {
client: connection,
rows: fields ? rows : [],
fields: _.map(fields, 'name'),
};
});
}),
});
MySQLAdapter.reopenClass(/** @lends MySQLAdapter */ {
Grammar: require('./grammar'),
Phrasing: require('./phrasing'),
Translator: require('./translator'),
Procedures: require('./procedures'),
});
MySQLAdapter.Phrasing.reopen(EmbedPseudoReturn);
MySQLAdapter.reopen(ExtractPseudoReturn);
module.exports = MySQLAdapter.reopenClass({ __name__: 'MySQLAdapter' });