From ad2a44d7b9fd909e2ddf1ecde7641a3cacbcb363 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Tue, 11 Oct 2016 11:58:11 +0800 Subject: [PATCH] fix: add try in makeSql --- lib/adapters/mysql.js | 132 ++++++++++++++++++++++++++++++------------ 1 file changed, 95 insertions(+), 37 deletions(-) diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index 4a5ba54..1021f71 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -544,7 +544,14 @@ class MySQLAdapter extends Adapter { } findWithNoCache(model, callback, options) { - const sql = this.makeSql("find", model, options); + let sql; + try { + sql = this.makeSql("find", model, options); + } catch(e) { + return process.nextTick(function() { + callback(e); + }); + } this.execute(sql, function(err, rows) { if(err) { return callback(err, undefined, sql); @@ -559,17 +566,32 @@ class MySQLAdapter extends Adapter { } findWithCache(cache, model, callback, options) { + options = options || {}; const self = this; const primaryKeys = model.primaryKeys.map(field => field.name); - const origFields = _.uniq((options.fields || []).concat(primaryKeys)); + const totalFields = model.schema.map(field => field.name); + const origFields = _.uniq((options.fields || totalFields).concat(primaryKeys)); // search for primary keys options.fields = primaryKeys; - const pkSQL = this.makeSql("find", model, options); + let pkSQL; + try { + pkSQL = this.makeSql("find", model, options); + } catch(e) { + return process.nextTick(function() { + return callback(e, undefined, ""); + }); + } this.execute(pkSQL, function(err, rows) { if(err) { options.fields = origFields; - return callback(err, undefined, self.makeSql("find", model, options)); + let sql; + try { + sql = self.makeSql("find", model, options); + } catch(e) { + return callback(e, undefined, ""); + } + return callback(err, undefined, sql); } // get data from cache first @@ -582,10 +604,18 @@ class MySQLAdapter extends Adapter { function fetchFromMySQL(taskObject) { const idx = taskObject.task; - const sql = self.makeSql("find", model, { - where: model.convertColumnToName(rows[idx]), - limit: [ 0, 1 ] - }); + + let sql; + try { + sql = self.makeSql("find", model, { + where: model.convertColumnToName(rows[idx]), + limit: [ 0, 1 ], + fields: totalFields + }); + } catch(e) { + errors.push(e); + return taskObject.done(); + } self.execute(sql, function(err, row) { if(err) { @@ -654,7 +684,14 @@ class MySQLAdapter extends Adapter { }); options.fields = origFields; - const extraSql = self.makeSql("find", model, options); + let extraSql; + try { + extraSql = self.makeSql("find", model, options); + } catch(e) { + return process.nextTick(function() { + return callback(e); + }); + } if(options.single) { return callback(err, liteResult.length ? liteResult[0] : null, extraSql); @@ -691,7 +728,7 @@ class MySQLAdapter extends Adapter { count(query, callback) { const _options = this.queryToOptions(query, {}); - const sql = this.makeSet("count", query.model, _options); + const sql = this.makeSql("count", query.model, _options); this.execute(sql, function(err, rows) { if(err) return callback(err, undefined, sql); return callback(undefined, (rows || [ { "COUNT(0)": 0 } ])[0]["COUNT(0)"], sql); @@ -710,8 +747,15 @@ class MySQLAdapter extends Adapter { updateByQuery(query, callback) { const self = this; const options = this.queryToOptions(query); - const sql = this.makeSql("update", query.model, options); const model = query.model; + let sql; + try { + sql = this.makeSql("update", query.model, options); + } catch(e) { + return process.nextTick(function() { + return callback(e); + }); + } let primaryKeys; async.waterfall([ @@ -725,7 +769,14 @@ class MySQLAdapter extends Adapter { primaryKeys = model.primaryKeys.map(key => key.name); options.fields = primaryKeys; - const relatedSql = self.makeSql("find", query.model, options); + let relatedSql; + try { + relatedSql = self.makeSql("find", query.model, options); + } catch(e) { + return process.nextTick(function() { + callback(e); + }); + } debug("find related rows when updating", relatedSql); self.execute(relatedSql, function(err, result) { if(err) return callback(err); @@ -749,8 +800,15 @@ class MySQLAdapter extends Adapter { deleteByQuery(query, callback) { const self = this; const options = this.queryToOptions(query); - const sql = this.makeSql("delete", query.model, options); const model = query.model; + let sql; + try { + sql = this.makeSql("delete", query.model, options); + } catch(e) { + return process.nextTick(function() { + return callback(e); + }); + } let primaryKeys; async.waterfall([ @@ -764,7 +822,15 @@ class MySQLAdapter extends Adapter { primaryKeys = model.primaryKeys.map(key => key.name); options.fields = primaryKeys; - const relatedSql = self.makeSql("find", query.model, options); + let relatedSql; + + try { + relatedSql = self.makeSql("find", query.model, options); + } catch(e) { + return process.nextTick(function() { + return callback(e); + }); + } debug("find related rows when deleting", relatedSql); self.execute(relatedSql, function(err, result) { if(err) return callback(err); @@ -903,23 +969,8 @@ class MySQLAdapter extends Adapter { return _set; }, []); - const _updateWhere = Object.keys(pk).reduce((where, key) => { - const data = pk[key]; - const field = model.fieldNamesMap[key]; - if(!field) return where; - - if(data === null) { - where.push(`\`${field.column}\` = NULL`); - } else if(field.needQuotes) { - where.push(`\`${field.column}\` = ${SqlString.escape(field.restore(data))}`); - } else { - where.push(`\`${field.column}\` = ${field.restore(data)}`); - } - - return where; - }, []); - - if(!_updateWhere.length) { + const _updateWhere = this.makeWhere(model, pk, "and"); + if(_updateWhere === "()") { return process.nextTick(function() { callback(new Error("Broken yukari object.")); }); @@ -933,17 +984,24 @@ class MySQLAdapter extends Adapter { const self = this; const pkNames = model.primaryKeys.map(key => key.name); - const sql = `UPDATE \`${model.name}\` SET ${_set.join(", ")} WHERE ${_updateWhere.join(" AND ")}`; + const sql = `UPDATE \`${model.name}\` SET ${_set.join(", ")} WHERE ${_updateWhere}`; async.waterfall([ function(callback) { if(!model.cache) return callback(); // delete cache if has - const relatedSql = self.makeSql("find", model, { - where: pk, - limit: [ 0, 1 ], - fields: pkNames - }); + let relatedSql; + try { + relatedSql = self.makeSql("find", model, { + where: pk, + limit: [ 0, 1 ], + fields: pkNames + }); + } catch(e) { + return process.nextTick(function() { + callback(e); + }); + } debug("find related row when updating Yukari", relatedSql); self.execute(relatedSql, function(err, result) { if(err) return callback(err);