Skip to content

Commit

Permalink
Add support for UniqueIdentifier (GUID) in result set rows.
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
pekim committed Feb 5, 2012
1 parent b2ac94c commit a7b5a17
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/token/data-type.coffee
Expand Up @@ -40,6 +40,10 @@ TYPE =
name: 'BigInt' name: 'BigInt'


# Variable-length types # Variable-length types
0x24:
type: 'GUIDN'
name: 'UniqueIdentifierN'
dataLengthLength: 1
0x26: 0x26:
type: 'INTN' type: 'INTN'
name: 'IntN' name: 'IntN'
Expand Down Expand Up @@ -99,11 +103,6 @@ TYPE =


### ###
# Variable-length types # Variable-length types
GUIDTYPE:
id: 0x24
variableLength: true
name: 'UniqueIdentifier'
dataLengthLength: 1
GUIDTYPE: 0x24 # UniqueIdentifier GUIDTYPE: 0x24 # UniqueIdentifier
DECIMALTYPE: 0x37 # Decimal (legacy support) DECIMALTYPE: 0x37 # Decimal (legacy support)
NUMERICTYPE: 0x3F # Numeric (legacy support) NUMERICTYPE: 0x3F # Numeric (legacy support)
Expand Down
8 changes: 8 additions & 0 deletions lib/token/row-token-parser.coffee
Expand Up @@ -137,6 +137,14 @@ parser = (buffer, columnsMetaData) ->


value *= sign value *= sign
value /= Math.pow(10, columnMetaData.scale) value /= Math.pow(10, columnMetaData.scale)
when 'UniqueIdentifierN'
switch dataLength
when 0
value = null
when 0x10
value = buffer.readArray(0x10)
else
throw new Error(sprintf('Unsupported guid size %d at offset 0x%04X', dataLength - 1, buffer.position))
else else
throw new Error(sprintf('Unrecognised column type %s at offset 0x%04X', type.name, buffer.position)) throw new Error(sprintf('Unrecognised column type %s at offset 0x%04X', type.name, buffer.position))
break break
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions test/integration/datatypes-in-results-test.coffee
Expand Up @@ -133,6 +133,14 @@ exports.nchar = (test) ->
exports.ncharNull = (test) -> exports.ncharNull = (test) ->
execSql(test, "select cast(null as nchar(5))", null) execSql(test, "select cast(null as nchar(5))", null)


exports.guid = (test) ->
execSql(test, "select cast('01234567-89AB-CDEF-0123-456789ABCDEF' as uniqueidentifier)", [
0x67, 0x45, 0x23, 0x01, 0xAB, 0x89, 0xEF, 0xCD,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
])

exports.guidNull = (test) ->
execSql(test, "select cast(null as uniqueidentifier)", null)


execSql = (test, sql, expectedValue) -> execSql = (test, sql, expectedValue) ->
test.expect(2) test.expect(2)
Expand Down
22 changes: 22 additions & 0 deletions test/unit/token/row-token-parser-test.coffee
Expand Up @@ -339,6 +339,28 @@ module.exports.intN = (test) ->


test.done() test.done()


module.exports.guidN = (test) ->
colMetaData = [
{type: dataTypeByName.UniqueIdentifierN}
{type: dataTypeByName.UniqueIdentifierN}
]

buffer = new WritableTrackingBuffer(0, 'ucs2')
buffer.writeBuffer(new Buffer([
0,
16, 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
]))
#console.log(buffer.data)

token = parser(new ReadableTrackingBuffer(buffer.data, 'ucs2'), colMetaData)
#console.log(token)

test.strictEqual(token.columns.length, 2)
test.strictEqual(token.columns[0].value, null)
test.deepEqual([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef], token.columns[1].value)

test.done()

module.exports.floatN = (test) -> module.exports.floatN = (test) ->
colMetaData = [ colMetaData = [
{type: dataTypeByName.FloatN} {type: dataTypeByName.FloatN}
Expand Down

1 comment on commit a7b5a17

@zachaller
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to say thanks a bunch. we are creating an api for some legacy software doing it in node but need to connect to ms sql and this helps.

Please sign in to comment.