Refactor the runQuery logic into a function

This commit is contained in:
Raymond Feng 2013-11-27 09:08:03 -08:00
parent f171e74392
commit 22b9e78692
1 changed files with 16 additions and 23 deletions

View File

@ -125,6 +125,19 @@ MySQL.prototype.query = function (sql, callback) {
callback && callback(err, result);
}
function runQuery(connection) {
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);
});
}
client.getConnection(function (err, connection) {
if (err) {
callback && callback(err);
@ -141,9 +154,7 @@ MySQL.prototype.query = function (sql, callback) {
connection.query(q, function (err) {
if (!err) {
connection.query('USE `' + db + '`', function (err) {
connection.query(sql, function (err, result) {
releaseConnectionAndCallback(connection, err, result);
});
runQuery(connection);
});
} else {
releaseConnectionAndCallback(connection, err);
@ -155,29 +166,11 @@ MySQL.prototype.query = function (sql, callback) {
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);
});
runQuery(connection);
});
} else {
// Bypass USE db
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);
});
runQuery(connection);
}
});
};