我正在尝试了解某人的代码,他在其中为SQL创建了自定义帮助程序功能
这是他使用的相关代码
having
这里conn是人从另一条路由通过的sql连接
设为:select client
from t
group by client
having min(admin_flag) = 'N' and max(admin_flag) = 'N';
现在我无法理解的神奇的DataBaseHelper(和函数)就是这个
exports.getCommission = function(conn, o, type) {
let table = "order_commissions";
if (type == "live") {
table = "order_commissions_live";
}
let sql = `select o.*, p.title, p.first_name, p.middle_name, p.last_name from ${table} o left join distributor_geneology dg on (o.fcid=dg.fcid) left join profile p on (p.user_id=dg.user_id) where o.order_no=? order by ledger_type, for_rank`;
return DatabaseHelper.getSql(conn, sql, [o.order_no]);
}
[问题] 有人可以帮助我了解这里的流程吗?像功能getSql在这里如何工作?
答案 0 :(得分:1)
好的,我可以指导您:
function getSql(connection, sql, params, rowno) {
// This line uses the Q library to setup a Promise, which is an alternative async control approach to callbacks (the Nodejs default approach)
var dfd = Q.defer();
// Take the provided connection and start a query. The anonymous function here is the callback expected by the library's connection.query() function signature
connection.query(sql, params, function(err, res) {
// nodejs callbacks usually use the error-first standard, so if err exists (i.e. null will return false in the 'if' statement), we reject the Promise with the error. The calling code will need to handle it with the .catch() function.
if (err) {
return dfd.reject(err);
}
// just a stdout log statement.
console.log("abc" + res)
// rowno is apparently intended to be an optional parameter to the getSql function. If it's undefined, we just want to resolve the Promise and send the calling code the full query response. That'll be passed to the .then() function on the Promise.
if (typeof rowno == 'undefined')
return dfd.resolve(res);
// I don't actually agree that this should return an error, but the author is saying that if the query returns no results, it should be an error rather than just an empty array. So again we Reject the Promise to send an error.
if (!res.length){
return dfd.reject(new Error("No rows found"));
}
// If we get here, then we know that both there are rows and the calling code wanted a specific row. This check is seeing if the rowno (row number) exists in the result set. Again, throwing an error if not (which, again, to me isn't an error).
if (!res[rowno]) {
return dfd.reject(new Error("No row found"));
}
// If we get here, we Resolve the Promise and pass the .then() function the requested row.
return dfd.resolve(res[rowno]);
});
// This is *outside* that anonymous function (the callback from the query call), and what you're doing is passing the calling code the actual Promise object so that it can chain the .then() and the .catch().
return dfd.promise;
}
好吧,因此,您将其命名为:
getCommission(conn, o, type).then(results => {
console.log(results); // or however you want to handle the results
}).then(e => {
console.error(e); // or however you want to handle an error.
});
所有这些,示例代码在我看来是非常糟糕。首先,他使用的字符串插值可能是一个讨厌的SQL注入向量,并且我还提到了一些错误情况是多么奇怪。