Skip to content

Commit 3a0e645

Browse files
authored
Merge pull request #351 from sushantdhiman/fix-347
enabled use of global typeCast
2 parents 2de6f24 + 8c7915a commit 3a0e645

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

lib/compile_text_parser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ function compile (fields, options, config) {
3535
result.push(' var result = new Array(' + fields.length + ')');
3636
}
3737

38+
// use global typeCast if current query doesn't specify one
39+
if (typeof config.typeCast === 'function' && typeof options.typeCast !== 'function') {
40+
options.typeCast = config.typeCast;
41+
}
42+
3843
if (typeof options.typeCast === 'function') {
3944
result.push(' var wrap = ' + wrap.toString());
4045
}

test/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ module.exports.createConnection = function (args, callback) {
7272
compress: (args && args.compress) || config.compress,
7373
decimalNumbers: args && args.decimalNumbers,
7474
dateStrings: args && args.dateStrings,
75-
authSwitchHandler: args && args.authSwitchHandler
75+
authSwitchHandler: args && args.authSwitchHandler,
76+
typeCast: args && args.typeCast
7677
};
7778

7879
//console.log('cc params', params);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var typeCastWrapper = function (stringMethod) {
2+
return function (field, next) {
3+
if (field.type == 'VAR_STRING') {
4+
return field.string()[stringMethod]();
5+
}
6+
return next();
7+
};
8+
};
9+
10+
var common = require('../../common');
11+
var connection = common.createConnection({
12+
typeCast: typeCastWrapper('toUpperCase')
13+
});
14+
15+
var assert = require('assert');
16+
17+
// query option override global typeCast
18+
connection.query({
19+
sql: 'select "FOOBAR" as foo',
20+
typeCast: typeCastWrapper('toLowerCase')
21+
}, function(err, res) {
22+
assert.ifError(err);
23+
assert.equal(res[0].foo, 'foobar')
24+
});
25+
26+
// global typecast works
27+
connection.query({
28+
sql: 'select "foobar" as foo',
29+
}, function(err, res) {
30+
assert.ifError(err);
31+
assert.equal(res[0].foo, 'FOOBAR');
32+
});
33+
34+
connection.end();

0 commit comments

Comments
 (0)