Skip to content

Commit

Permalink
fix: don't redefined sqlstring.escape (ali-sdk#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Sep 26, 2017
1 parent 004713c commit 5ca4489
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/sqlstring.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const Literal = require('./literals').Literal;

module.exports = SqlString;

SqlString.__escape = SqlString.escape;
if (!SqlString.__escape) {
SqlString.__escape = SqlString.escape;

SqlString.escape = function(val, stringifyObjects, timeZone) {
if (val instanceof Literal) {
return val.toString();
}
return SqlString.__escape(val, stringifyObjects, timeZone);
};
SqlString.escape = function(val, stringifyObjects, timeZone) {
if (val instanceof Literal) {
return val.toString();
}
return SqlString.__escape(val, stringifyObjects, timeZone);
};
}
9 changes: 9 additions & 0 deletions test/sqlstring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ describe('sqlstring.test.js', function() {
assert.equal(SqlString.escape(literals.Literal('sum2()')), 'sum2()');
assert.equal(SqlString.escape(literals.now), mysql.escape(literals.now));
});

it('should only patch once', function() {
require.cache[require.resolve('../lib/sqlstring')] = null;
const AnotherSqlString = require('../lib/sqlstring');
assert.equal(AnotherSqlString.escape(literals.now), 'now()');
assert.equal(AnotherSqlString.escape(new literals.Literal('sum')), 'sum');
assert.equal(AnotherSqlString.escape(literals.Literal('sum2()')), 'sum2()');
assert.equal(AnotherSqlString.escape(literals.now), mysql.escape(literals.now));
});
});
});

0 comments on commit 5ca4489

Please sign in to comment.