From 60674f983811037002a7ed0c83dc29920dc971cd Mon Sep 17 00:00:00 2001 From: tobiasnickel Date: Sat, 18 Jun 2016 14:42:42 +0800 Subject: [PATCH 1/2] provide the lib-users call-stack. before, users of bearcat-dao get an error from mysql-lib, but don't know what of there code coursed that error. --- lib/template/sql/mysqlTemplate.js | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/template/sql/mysqlTemplate.js b/lib/template/sql/mysqlTemplate.js index 8327012..9536afe 100644 --- a/lib/template/sql/mysqlTemplate.js +++ b/lib/template/sql/mysqlTemplate.js @@ -143,8 +143,9 @@ MysqlTemplate.prototype.getIdTableName = function(idTableName) { MysqlTemplate.prototype.allocateRecordId = function(tableName, cb) { var self = this; var idTableName = this.getIdTableName(); + var userErrorStack = new Error().stack; this.getConnection(function(err, connection, tx) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } var sql = 'UPDATE ' + idTableName + ' SET id = LAST_INSERT_ID(id + 1) WHERE name = ?'; @@ -152,7 +153,7 @@ MysqlTemplate.prototype.allocateRecordId = function(tableName, cb) { if (!tx) { self.connectionManager.release(connection); } - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } var id = -1; @@ -163,7 +164,7 @@ MysqlTemplate.prototype.allocateRecordId = function(tableName, cb) { if (id === -1) { sql = 'INSERT INTO ' + idTableName + ' (name, id) values (?, 0)'; self.executeUpdate(sql, [tableName], function(err) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } @@ -224,9 +225,9 @@ MysqlTemplate.prototype.batchAddRecords = function(sql, paramList, cb) { */ MysqlTemplate.prototype.existRecord = function(sql, params, cb) { var self = this; - + var userErrorStack = new Error().stack; this.getConnection(function(err, connection, tx) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return; } @@ -234,7 +235,7 @@ MysqlTemplate.prototype.existRecord = function(sql, params, cb) { if (!tx) { self.connectionManager.release(connection); } - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } if (results && results.length) { @@ -294,8 +295,9 @@ MysqlTemplate.prototype.batchUpdateRecords = function(sql, paramList, cb) { */ MysqlTemplate.prototype.queryCount = function(sql, params, cb) { var self = this; + var userErrorStack = new Error().stack; this.getConnection(function(err, connection, tx) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return; } @@ -303,7 +305,7 @@ MysqlTemplate.prototype.queryCount = function(sql, params, cb) { if (!tx) { self.connectionManager.release(connection); } - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } if (results && results.length) { @@ -327,8 +329,9 @@ MysqlTemplate.prototype.queryCount = function(sql, params, cb) { */ MysqlTemplate.prototype.executeQuery = function(sql, params, cb) { var self = this; + var userErrorStack = new Error().stack; this.getConnection(function(err, connection, tx) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } @@ -336,7 +339,7 @@ MysqlTemplate.prototype.executeQuery = function(sql, params, cb) { if (!tx) { self.connectionManager.release(connection); } - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } cb(null, results); @@ -467,8 +470,9 @@ MysqlTemplate.prototype.directUpdateById = function(object, options, cb) { */ MysqlTemplate.prototype.executeUpdate = function(sql, params, cb) { var self = this; + var userErrorStack = new Error().stack; this.getConnection(function(err, connection, tx) { - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } @@ -476,7 +480,7 @@ MysqlTemplate.prototype.executeUpdate = function(sql, params, cb) { if (!tx) { self.connectionManager.release(connection); } - if (self.handleError(err, cb)) { + if (self.handleError(err, userErrorStack, cb)) { return self; } if (result && result['affectedRows']) { @@ -536,8 +540,15 @@ MysqlTemplate.prototype.print = function(sql) { * @param {Function} cb callback function * @api private */ -MysqlTemplate.prototype.handleError = function(err, cb) { +MysqlTemplate.prototype.handleError = function(err, userErrorStack, cb) { + if(typeof userErrorStack == 'function'){ + cb = userErrorStack; + userErrorStack = ''; + } if (err) { + if(err instanceof Error){ + err.stack += '\n'+userErrorStack; + } cb(err); return true; } From ad757bdf08992b729d98cc25df09444f977d2267 Mon Sep 17 00:00:00 2001 From: tobiasnickel Date: Sat, 18 Jun 2016 16:04:39 +0800 Subject: [PATCH 2/2] optional params in our team, we had a few times the problem, that the developer want to execute a specific query, that is not using any parameter. now it is necessary to pass an empty object or array to the methods. (mostly get list) but they will not receive a Bearcat object, but directly the mySQL result. it will also course that the used mySQL-connection will not get released to the pool. (is locket forever) --- lib/core/domainDaoSupport.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/core/domainDaoSupport.js b/lib/core/domainDaoSupport.js index b57e538..b176e5b 100644 --- a/lib/core/domainDaoSupport.js +++ b/lib/core/domainDaoSupport.js @@ -696,6 +696,11 @@ DomainDaoSupport.prototype.getList = function(sql, params, options, cb) { if (Utils.isNotNull(limit) && Utils.isNotNull(offset)) { sql = SqlBuilderUtil.appendLimit(sql, limit, offset); } + + if(Utils.checkFunction(params)){ + cb = params; + params = []; + } return this.getSqlTemplate().executeQuery(sql, params, function(err, results) { if (err) { @@ -797,6 +802,11 @@ DomainDaoSupport.prototype.get = function(sql, params, cb) { var mid = tableConfig.getMid(); var func = tableConfig.getFunc(); + if(Utils.checkFunction(params)){ + cb = params; + params = []; + } + return this.getSqlTemplate().executeQuery(sql, params, function(err, results) { if (err) { return cb(err); @@ -825,6 +835,11 @@ DomainDaoSupport.prototype.get = function(sql, params, cb) { * @api public */ DomainDaoSupport.prototype.getCount = function(sql, params, cb) { + if(Utils.checkFunction(params)){ + cb = params; + params = []; + } + sql = SqlBuilderUtil.getSql(sql); return this.getSqlTemplate().queryCount(sql, params, cb); } @@ -907,6 +922,11 @@ DomainDaoSupport.prototype.update = function(sql, params, cb) { * @api public */ DomainDaoSupport.prototype.exists = function(sql, params, cb) { + if(Utils.checkFunction(params)){ + cb = params; + params = []; + } + sql = SqlBuilderUtil.getSql(sql); return this.getSqlTemplate().existRecord(sql, params, cb); }