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); 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) { client.getConnection(function (err, connection) {
if (err) { if (err) {
callback && callback(err); callback && callback(err);
@ -141,9 +154,7 @@ MySQL.prototype.query = function (sql, callback) {
connection.query(q, function (err) { connection.query(q, function (err) {
if (!err) { if (!err) {
connection.query('USE `' + db + '`', function (err) { connection.query('USE `' + db + '`', function (err) {
connection.query(sql, function (err, result) { runQuery(connection);
releaseConnectionAndCallback(connection, err, result);
});
}); });
} else { } else {
releaseConnectionAndCallback(connection, err); releaseConnectionAndCallback(connection, err);
@ -155,29 +166,11 @@ MySQL.prototype.query = function (sql, callback) {
return; return;
} }
} }
connection.query(sql, function (err, data) { runQuery(connection);
if (debug) {
if (err) {
console.error('Error:', err);
}
console.log('Data:', data);
}
if (log) log(sql, time);
releaseConnectionAndCallback(connection, err, data);
});
}); });
} else { } else {
// Bypass USE db // Bypass USE db
connection.query(sql, function (err, data) { runQuery(connection);
if (debug) {
if (err) {
console.error('Error:', err);
}
console.log('Data:', data);
}
if (log) log(sql, time);
releaseConnectionAndCallback(connection, err, data);
});
} }
}); });
}; };