Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user error stack, optional params #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/core/domainDaoSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
37 changes: 24 additions & 13 deletions lib/template/sql/mysqlTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,17 @@ 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 = ?';
var query = connection.query(sql, [tableName], function(err, results) {
if (!tx) {
self.connectionManager.release(connection);
}
if (self.handleError(err, cb)) {
if (self.handleError(err, userErrorStack, cb)) {
return self;
}
var id = -1;
Expand All @@ -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;
}

Expand Down Expand Up @@ -224,17 +225,17 @@ 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;
}

var query = connection.query(sql, params, function(err, results) {
if (!tx) {
self.connectionManager.release(connection);
}
if (self.handleError(err, cb)) {
if (self.handleError(err, userErrorStack, cb)) {
return self;
}
if (results && results.length) {
Expand Down Expand Up @@ -294,16 +295,17 @@ 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;
}

var query = connection.query(sql, params, function(err, results) {
if (!tx) {
self.connectionManager.release(connection);
}
if (self.handleError(err, cb)) {
if (self.handleError(err, userErrorStack, cb)) {
return self;
}
if (results && results.length) {
Expand All @@ -327,16 +329,17 @@ 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;
}

var query = connection.query(sql, params, function(err, results) {
if (!tx) {
self.connectionManager.release(connection);
}
if (self.handleError(err, cb)) {
if (self.handleError(err, userErrorStack, cb)) {
return self;
}
cb(null, results);
Expand Down Expand Up @@ -467,16 +470,17 @@ 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;
}

var query = connection.query(sql, params, function(err, result) {
if (!tx) {
self.connectionManager.release(connection);
}
if (self.handleError(err, cb)) {
if (self.handleError(err, userErrorStack, cb)) {
return self;
}
if (result && result['affectedRows']) {
Expand Down Expand Up @@ -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;
}
Expand Down