Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix geography v2 parsing #322 #1138

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})