Skip to content

Commit

Permalink
Merge pull request #47 from wshager/master
Browse files Browse the repository at this point in the history
Fixed SQL storing/retrieving in UTF-8
  • Loading branch information
kriszyp committed Nov 18, 2013
2 parents 72d613d + 4e8859b commit 5c7796d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
24 changes: 17 additions & 7 deletions engines/node/store-engine/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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');

Expand Down
5 changes: 5 additions & 0 deletions errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
5 changes: 3 additions & 2 deletions store/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5c7796d

Please sign in to comment.