diff --git a/engines/node/store-engine/sql.js b/engines/node/store-engine/sql.js index d0f2ee9..d5bfbe3 100644 --- a/engines/node/store-engine/sql.js +++ b/engines/node/store-engine/sql.js @@ -4,7 +4,8 @@ * Currently only supports MySQL. */ -var DatabaseError = require('perstore/errors').DatabaseError; +var DatabaseError = require('perstore/errors').DatabaseError, + DuplicateEntryError = require('perstore/errors').DuplicateEntryError; var engines = { mysql: MysqlWrapper @@ -36,19 +37,26 @@ function MysqlWrapper(params) { if(charset && charset.name=="utf8") conn.execute("SET NAMES utf8"); var cmd = conn.execute(query,args); - cmd.on('result', function() { + // use result from callback + cmd.on('result', function(result) { if (conn.clean && callback) { callback({ - insertId: cmd.result.insert_id, - rowsAffected: cmd.result.affected_rows, - rows: cmd.result.rows + insertId: result.insert_id, + rowsAffected: result.affected_rows, + rows: result.rows }); } }); cmd.on('error', function(err) { conn.clean = false; - if (errback) - errback(err); + if(errback) { + var patt=/^duplicate entry/ig; + if(err && patt.test(err.message)) { + errback(new DuplicateEntryError(err.message)); + } else { + errback(err); + } + } }); }, transaction: function() { @@ -91,6 +99,8 @@ function MysqlWrapper(params) { ret.row_as_hash = true; ret.clean = true; + // use charset if available + if(params.charset) ret.set("charset",params.charset); throwOnError(ret.connection, 'connect to DB'); throwOnError(ret.auth(params.name, params.username, params.password), 'authenticate'); diff --git a/errors.js b/errors.js index c08423e..7bd97ac 100644 --- a/errors.js +++ b/errors.js @@ -10,3 +10,8 @@ NotFoundError.prototype.code = 2; var PreconditionFailed = exports.PreconditionFailed = ErrorConstructor("PreconditionFailed", DatabaseError); PreconditionFailed.prototype.code = 3; + +var DuplicateEntryError = exports.DuplicateEntryError = ErrorConstructor("DuplicateEntryError", DatabaseError); +DuplicateEntryError.prototype.code = 4; + +var AcceptError = exports.AcceptError = ErrorConstructor("AcceptError"); \ No newline at end of file diff --git a/store/mongodb.js b/store/mongodb.js index 23c1e30..48faf2c 100644 --- a/store/mongodb.js +++ b/store/mongodb.js @@ -20,7 +20,8 @@ var convertNodeAsyncFunction = require('promised-io/promise').convertNodeAsyncFu defer = require("promised-io/promise").defer, when = require("promised-io/promise").when, jsArray = require("rql/js-array"), - PreconditionFailed = require("../errors").PreconditionFailed; + PreconditionFailed = require("../errors").PreconditionFailed, + DuplicateEntryError = require('perstore/errors').DuplicateEntryError; var RQ = require("rql/parser"); //RQ.converters["default"] = exports.converters.auto; @@ -279,7 +280,7 @@ module.exports = function(options){ deferred.resolve(obj.id); }); } else { - deferred.reject(id + " exists, and can't be overwritten"); + deferred.reject(new DuplicateEntryError(id + " exists, and can't be overwritten")); } }); } else {