Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add guid support for parameterization
Signed-off-by: Zach Aller <zachaller@hotmail.com>
  • Loading branch information
zachaller committed Aug 9, 2012
1 parent b74265a commit cecf81f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
47 changes: 46 additions & 1 deletion src/data-type.coffee
@@ -1,3 +1,4 @@
guidParser = require('./guid-parser')
NULL = (1 << 16) - 1
EPOCH_DATE = new Date(1900, 0, 1)
MAX = (1 << 16) - 1
Expand Down Expand Up @@ -97,6 +98,25 @@ TYPE =
0x3B:
type: 'FLT4'
name: 'Real'
# Had some weired rounding issues not sure what was going on
###
declaration: (parameter) ->
'real'
writeParameterData: (buffer, parameter) ->
# ParamMetaData (TYPE_INFO)
# Some issues with rounding
buffer.writeUInt8(typeByName.FloatN.id)
buffer.writeUInt8(4)
# ParamLenData
if parameter.value?
buffer.writeUInt8(4)
buffer.writeFloatLE(parameter.value)
else
buffer.writeUInt8(0)
###
0x3C:
type: 'MONEY'
name: 'Money'
Expand Down Expand Up @@ -130,7 +150,19 @@ TYPE =
0x3E:
type: 'FLT8'
name: 'Float'

declaration: (parameter) ->
'float'
writeParameterData: (buffer, parameter) ->
# ParamMetaData (TYPE_INFO)
buffer.writeUInt8(typeByName.FloatN.id)
buffer.writeUInt8(8)

# ParamLenData
if parameter.value?
buffer.writeUInt8(8)
buffer.writeDoubleLE(parameter.value)
else
buffer.writeUInt8(0)
0x7A:
type: 'MONEY4'
name: 'SmallMoney'
Expand Down Expand Up @@ -170,6 +202,19 @@ TYPE =
type: 'GUIDN'
name: 'UniqueIdentifierN'
dataLengthLength: 1
declaration: (parameter) ->
'uniqueidentifier'
writeParameterData: (buffer, parameter) ->
# ParamMetaData (TYPE_INFO)
buffer.writeUInt8(typeByName.UniqueIdentifierN.id)
buffer.writeUInt8(0x10)

# ParamLenData
if parameter.value?
buffer.writeUInt8(0x10)
buffer.writeBuffer(new Buffer(guidParser.guidToArray(parameter.value)))
else
buffer.writeUInt8(0)
0x26:
type: 'INTN'
name: 'IntN'
Expand Down
24 changes: 24 additions & 0 deletions src/guid-parser.coffee
@@ -0,0 +1,24 @@
guidToArray = (guid) ->
b1 = parseInt(guid.substring(6,8), 16)
b2 = parseInt(guid.substring(4,6), 16)
b3 = parseInt(guid.substring(2,4), 16)
b4 = parseInt(guid.substring(0,2), 16)
b5 = parseInt(guid.substring(11,13), 16)
b6 = parseInt(guid.substring(9,11), 16)
b7 = parseInt(guid.substring(16,18), 16)
b8 = parseInt(guid.substring(14,16), 16)

b9 = parseInt(guid.substring(19,21), 16)
b10 = parseInt(guid.substring(21,23), 16)

b11 = parseInt(guid.substring(24,26), 16)
b12 = parseInt(guid.substring(26,28), 16)
b13 = parseInt(guid.substring(28,30), 16)
b14 = parseInt(guid.substring(30,32), 16)
b15 = parseInt(guid.substring(32,34), 16)
b16 = parseInt(guid.substring(34,36), 16)

final = [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16]

final
exports.guidToArray = guidToArray
12 changes: 12 additions & 0 deletions src/tracking-buffer/writable-tracking-buffer.coffee
Expand Up @@ -104,6 +104,18 @@ class WritableTrackingBuffer
@makeRoomFor(length)
@buffer.writeInt32BE(value, @position)
@position += length

writeFloatLE: (value) ->
length = 4
@makeRoomFor(length)
@buffer.writeFloatLE(value, @position)
@position += length

writeDoubleLE: (value) ->
length = 8
@makeRoomFor(length)
@buffer.writeDoubleLE(value, @position)
@position += length

writeString: (value, encoding) ->
encoding ||= @encoding
Expand Down
22 changes: 18 additions & 4 deletions test/integration/parameterised-statements-test.coffee
@@ -1,6 +1,7 @@
Connection = require('../../src/connection')
Request = require('../../src/request')
fs = require('fs')
guidParser = require('../../src/guid-parser')
TYPES = require('../../src/data-type').typeByName

getConfig = ->
Expand Down Expand Up @@ -46,13 +47,16 @@ exports.int = (test) ->
execSql(test, TYPES.Int, 8)

exports.bigint = (test) ->
execSql(test, TYPES.BigInt, 67553994410557440)
execSql(test, TYPES.BigInt, 18014402804449279)

exports.bigintsmall = (test) ->
execSql(test, TYPES.BigInt, 8)

# exports.bigintsmall = (test) ->
# execSql(test, TYPES.Float, 8.5)
exports.float = (test) ->
execSql(test, TYPES.Float, 9654.2546456567565767644)

exports.uniqueIdentifierN = (test) ->
execSql(test, TYPES.UniqueIdentifierN, '01234567-89AB-CDEF-0123-456789ABCDEF')

exports.intZero = (test) ->
execSql(test, TYPES.Int, 0)
Expand Down Expand Up @@ -133,6 +137,12 @@ exports.outputBigInt = (test) ->

exports.outputBigIntSmall = (test) ->
execSqlOutput(test, TYPES.BigInt, 8)

exports.outputFloat = (test) ->
execSqlOutput(test, TYPES.Float, 9654.2546456567565767644)

exports.outputUniqueIdentifierN = (test) ->
execSqlOutput(test, TYPES.UniqueIdentifierN, '01234567-89AB-CDEF-0123-456789ABCDEF')

exports.outputIntNull = (test) ->
execSqlOutput(test, TYPES.Int, null)
Expand Down Expand Up @@ -225,7 +235,9 @@ execSql = (test, type, value) ->
if (value instanceof Date)
test.strictEqual(columns[0].value.getTime(), value.getTime())
else if (type == TYPES.BigInt)
test.strictEqual(columns[0].value, value.toString())
test.strictEqual(columns[0].value, value.toString())
else if (type == TYPES.UniqueIdentifierN)
test.deepEqual(columns[0].value, guidParser.guidToArray(value))
else
test.strictEqual(columns[0].value, value)
)
Expand Down Expand Up @@ -274,6 +286,8 @@ execSqlOutput = (test, type, value) ->
test.strictEqual(returnValue.getTime(), value.getTime())
else if (type == TYPES.BigInt)
test.strictEqual(returnValue, value.toString())
else if (type == TYPES.UniqueIdentifierN)
test.deepEqual(returnValue, guidParser.guidToArray(value))
else
test.strictEqual(returnValue, value)

Expand Down

0 comments on commit cecf81f

Please sign in to comment.