knex/lib/dialects/mysql/index.js

106 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict';
// MySQL Client
// -------
var inherits = require('inherits');
var Client = require('../../client');
var Promise = require('../../promise');
var mysql;
// Always initialize with the "QueryBuilder" and "QueryCompiler"
// objects, which extend the base 'lib/query/builder' and
// 'lib/query/compiler', respectively.
function Client_MySQL(config) {
Client.call(this, config);
}
inherits(Client_MySQL, Client);
// The "dialect", for reference elsewhere.
Client_MySQL.prototype.dialect = 'mysql';
// Lazy-load the mysql dependency, since we might just be
// using the client to generate SQL strings.
Client_MySQL.prototype.initDriver = function() {
mysql = mysql || require('mysql');
};
// Attach a `Formatter` constructor to the client object.
Client_MySQL.prototype.initFormatter = function() {
require('./formatter')(this);
};
// Attaches the `Raw` constructor to the client object.
Client_MySQL.prototype.initRaw = function() {
require('./raw')(this);
};
// Attaches the `FunctionHelper` constructor to the client object.
Client_MySQL.prototype.initFunctionHelper = function() {
require('./functionhelper')(this);
};
// Attaches the `Transaction` constructor to the client object.
Client_MySQL.prototype.initTransaction = function() {
require('./transaction')(this);
};
// Attaches `QueryBuilder` and `QueryCompiler` constructors
// to the client object.
Client_MySQL.prototype.initQuery = function() {
require('./query')(this);
};
// Initialize the query "runner"
Client_MySQL.prototype.initRunner = function() {
require('./runner')(this);
};
// Lazy-load the schema dependencies; we may not need to use them.
Client_MySQL.prototype.initSchema = function() {
require('./schema')(this);
};
// Lazy-load the migration dependency
Client_MySQL.prototype.initMigrator = function() {
require('./migrator')(this);
};
// Lazy-load the seeding dependency
Client_MySQL.prototype.initSeeder = function() {
require('./seeder')(this);
};
// MySQL Specific error handler
function connectionErrorHandler(client, connection, err) {
if (connection && err && err.fatal) {
if (connection.__knex__disposed) return;
connection.__knex__disposed = true;
2015-02-19 11:53:57 -05:00
client.pool.destroy(connection);
}
}
// Get a raw connection, called by the `pool` whenever a new
// connection needs to be added to the pool.
2014-04-09 10:11:41 -04:00
Client_MySQL.prototype.acquireRawConnection = function() {
var client = this;
var connection = mysql.createConnection(this.connectionSettings);
this.databaseName = connection.config.database;
return new Promise(function(resolver, rejecter) {
connection.connect(function(err) {
2014-04-09 10:11:41 -04:00
if (err) return rejecter(err);
2014-10-01 18:28:30 -04:00
connection.on('error', connectionErrorHandler.bind(null, client, connection));
connection.on('end', connectionErrorHandler.bind(null, client, connection));
resolver(connection);
});
});
};
// Used to explicitly close a connection, called internally by the pool
// when a connection times out or the pool is shutdown.
Client_MySQL.prototype.destroyRawConnection = function(connection, cb) {
connection.end(cb);
};
2015-02-19 11:53:57 -05:00
module.exports = Client_MySQL;