diff --git a/lib/mysql.js b/lib/mysql.js index d61f8ce..9bfe733 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -31,22 +31,36 @@ exports.initialize = function initializeDataSource(dataSource, callback) { s.supportBigNumbers = (s.supportBigNumbers || false); s.timezone = (s.timezone || 'local'); - dataSource.client = mysql.createPool({ + if(isNaN(s.connectionLimit)) { + s.connectionLimit = s.connectionLimit; + } else { + s.connectionLimit = 10; + } + + var options = { host: s.host || s.hostname || 'localhost', port: s.port || 3306, user: s.username || s.user, password: s.password, timezone: s.timezone, - // database: s.database, // Don't configure the DB here so that the pool can be used for multiple DBs debug: s.debug, socketPath: s.socketPath, charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd. supportBigNumbers: s.supportBigNumbers, - connectionLimit: s.connectionLimit || 10 - }); + connectionLimit: s.connectionLimit + }; + + // Don't configure the DB if the pool can be used for multiple DBs + if(!s.createDatabase) { + options.database = s.database; + } + + dataSource.client = mysql.createPool(options); dataSource.client.on('error', function (err) { dataSource.emit('error', err); + dataSource.connected = false; + dataSource.connecting = false; }); if (s.debug) { @@ -105,49 +119,66 @@ MySQL.prototype.query = function (sql, callback) { if (debug) { console.log('SQL:', sql); } + + function releaseConnectionAndCallback(connection, err, result) { + connection.release(); + callback && callback(err, result); + } + client.getConnection(function (err, connection) { if (err) { callback && callback(err); return; } - connection.query('USE `' + db + '`', function (err) { - if (err) { - if (err && err.message.match(/(^|: )unknown database/i)) { - var charset = self.settings.charset; - var collation = self.settings.collation; - var q = 'CREATE DATABASE ' + db + ' CHARACTER SET ' + charset + ' COLLATE ' + collation; - connection.query(q, function (error) { - if (!error) { - connection.query('USE `' + db + '`', function (err) { - connection.query(sql, function (err, result) { - connection.release(); - callback && callback(err, result); + if (self.settings.createDatabase) { + // Call USE db ... + connection.query('USE `' + db + '`', function (err) { + if (err) { + if (err && err.message.match(/(^|: )unknown database/i)) { + var charset = self.settings.charset; + var collation = self.settings.collation; + var q = 'CREATE DATABASE ' + db + ' CHARACTER SET ' + charset + ' COLLATE ' + collation; + connection.query(q, function (err) { + if (!err) { + connection.query('USE `' + db + '`', function (err) { + connection.query(sql, function (err, result) { + releaseConnectionAndCallback(connection, err, result); + }); }); - }); - } else { - connection.release(); - callback && callback(err); - } - }); - return; - } else { - connection.release(); - callback && callback(err); - return; + } else { + releaseConnectionAndCallback(connection, err); + } + }); + return; + } else { + releaseConnectionAndCallback(connection, err); + return; + } } - } + connection.query(sql, function (err, data) { + if (debug) { + if (err) { + console.error('Error:', err); + } + console.log('Data:', data); + } + if (log) log(sql, time); + releaseConnectionAndCallback(connection, err, data); + }); + }); + } else { + // Bypass USE db connection.query(sql, function (err, data) { if (debug) { - if(err) { + if (err) { console.error('Error:', err); } console.log('Data:', data); } if (log) log(sql, time); - connection.release(); - callback && callback(err, data); + releaseConnectionAndCallback(connection, err, data); }); - }); + } }); }; @@ -1057,7 +1088,7 @@ function unsigned(p, dt) { } /** - * Disconnect from MongoDB + * Disconnect from MySQL */ MySQL.prototype.disconnect = function () { if(this.debug) { diff --git a/test/connection.test.js b/test/connection.test.js index c791bc4..fae9810 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -8,7 +8,7 @@ describe('migrations', function() { before(function() { require('./init.js'); - odb = getDataSource({collation: 'utf8_general_ci'}); + odb = getDataSource({collation: 'utf8_general_ci', createDatabase: true}); db = odb; }); @@ -57,7 +57,7 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done){ assert.ok(!err); odb.client.end(function(){ - db = getSchema({collation: test_set_collo}); + db = getSchema({collation: test_set_collo, createDatabase: true}); DummyModel = db.define('DummyModel', {string: String}); db.automigrate(function(){ var q = 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.client.escape(db.settings.database) + ' LIMIT 1'; diff --git a/test/init.js b/test/init.js index b455117..dac2e87 100644 --- a/test/init.js +++ b/test/init.js @@ -11,7 +11,8 @@ global.getConfig = function(options) { port: config.port || 3306, database: 'myapp_test', username: config.username, - password: config.password + password: config.password, + createDatabase: true }; if (options) {