Skip to content

Commit 2f59a23

Browse files
committed
Fix type inference of non-integer numbers (tediousjs#475)
Also included test against getTypeByValue to simplify future type inference confirmations
1 parent d542c4a commit 2f59a23

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/base.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ let getTypeByValue = function (value) {
6666
return TYPES.NVarChar
6767

6868
case 'number':
69-
for (item of Array.from(map)) {
70-
if (item.js === Number) {
71-
return item.sql
72-
}
69+
if (value % 1 === 0) {
70+
return TYPES.Int
71+
} else {
72+
return TYPES.Float
7373
}
7474

75-
return TYPES.Int
76-
7775
case 'boolean':
7876
for (item of Array.from(map)) {
7977
if (item.js === Boolean) {
@@ -1558,7 +1556,8 @@ module.exports = {
15581556
ISOLATION_LEVEL,
15591557
TYPES,
15601558
MAX: 65535, // (1 << 16) - 1
1561-
map
1559+
map,
1560+
getTypeByValue
15621561
}
15631562
}
15641563

test/common/unit.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,9 @@ describe('Unit', () => {
173173
done()
174174
})
175175
})
176+
177+
it('infer type by value', () => {
178+
assert.strictEqual(sql.Int, sql.getTypeByValue(23))
179+
assert.strictEqual(sql.Float, sql.getTypeByValue(1.23))
180+
})
176181
})

0 commit comments

Comments
 (0)