Skip to content

Commit

Permalink
fix geography v2 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mastodon0 committed Nov 18, 2020
1 parent 1e5e688 commit f504fcd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/udt.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const parseGeography = buffer => {

// console.log( "shapes", shapes)

if (value.version === 2) {
if (value.version === 2 && buffer.position < buffer.length) {
const numberOfSegments = buffer.readUInt32LE(buffer.position)
buffer.position += 4

Expand Down
39 changes: 39 additions & 0 deletions test/common/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const sql = require('../../')
const assert = require('assert')
const cs = require('../../lib/connectionstring')
const udt = require('../../lib/udt')

describe('Connection String', () => {
it('Connection String #1', done => {
Expand Down Expand Up @@ -286,3 +287,41 @@ describe('Unit', () => {
assert.strictEqual(req.parameters.param1.value, 123)
})
})

describe('Geography Parsing', () => {
it('polygon v1', () => {
// select geography::STGeomFromText(N'POLYGON((1 1, 3 1, 3 7, 1 1))',4326)
const buffer = Buffer.from('E6100000010404000000000000000000F03F000000000000F03F000000000000F03F00000000000008400000000000001C400000000000000840000000000000F03F000000000000F03F01000000020000000001000000FFFFFFFF0000000003', 'hex')
const geo = udt.PARSERS.geography(buffer)

assert.strictEqual(geo.version, 1)
assert.strictEqual(geo.srid, 4326)
assert.strictEqual(geo.points.length, 4)
assert.strictEqual(geo.points[0].y, 1)
assert.strictEqual(geo.points[0].x, 1)
assert.strictEqual(geo.points[1].y, 3)
assert.strictEqual(geo.points[1].x, 1)
assert.strictEqual(geo.points[2].y, 3)
assert.strictEqual(geo.points[2].x, 7)
assert.strictEqual(geo.points[3].y, 1)
assert.strictEqual(geo.points[3].x, 1)
})

it('polygon v2', () => {
// select geography::STGeomFromText(N'POLYGON((1 1, 3 1, 3 1, 1 1))',4326)
const buffer = Buffer.from('E6100000020004000000000000000000F03F000000000000F03F000000000000F03F0000000000000840000000000000F03F0000000000000840000000000000F03F000000000000F03F01000000010000000001000000FFFFFFFF0000000003', 'hex')
const geo = udt.PARSERS.geography(buffer)

assert.strictEqual(geo.version, 2)
assert.strictEqual(geo.srid, 4326)
assert.strictEqual(geo.points.length, 4)
assert.strictEqual(geo.points[0].y, 1)
assert.strictEqual(geo.points[0].x, 1)
assert.strictEqual(geo.points[1].y, 3)
assert.strictEqual(geo.points[1].x, 1)
assert.strictEqual(geo.points[2].y, 3)
assert.strictEqual(geo.points[2].x, 1)
assert.strictEqual(geo.points[3].y, 1)
assert.strictEqual(geo.points[3].x, 1)
})
})

0 comments on commit f504fcd

Please sign in to comment.