Improve the connector based on review feedbacks

Introduce a 'createDatabase' option to allow DB to created
Wrap the callback with connection.release
Fix typo
This commit is contained in:
Raymond Feng 2013-11-27 08:42:40 -08:00
parent aac933c39d
commit f171e74392
3 changed files with 68 additions and 36 deletions

View File

@ -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) {

View File

@ -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';

View File

@ -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) {