Permalink
Browse files

Merge pull request #45 from zachaller/master

Add parameter types (GUID, bigint, float)
  • Loading branch information...
2 parents 1c37071 + ef1c8b6 commit 2a498fbb74df790ea859e6e7a2001829208c80f7 @pekim pekim committed Sep 8, 2012
View
@@ -1,3 +1,4 @@
+guidParser = require('./guid-parser')
NULL = (1 << 16) - 1
EPOCH_DATE = new Date(1900, 0, 1)
MAX = (1 << 16) - 1
@@ -22,7 +23,7 @@ TYPE =
# ParamLenData
if parameter.value?
buffer.writeUInt8(1)
- buffer.writeInt8(parameter.value)
+ buffer.writeInt8(parseInt(parameter.value))
else
buffer.writeUInt8(0)
0x32:
@@ -54,7 +55,7 @@ TYPE =
# ParamLenData
if parameter.value?
buffer.writeUInt8(2)
- buffer.writeInt16LE(parameter.value)
+ buffer.writeInt16LE(parseInt(parameter.value))
else
buffer.writeUInt8(0)
0x38:
@@ -70,7 +71,7 @@ TYPE =
# ParamLenData
if parameter.value?
buffer.writeUInt8(4)
- buffer.writeInt32LE(parameter.value)
+ buffer.writeInt32LE(parseInt(parameter.value))
else
buffer.writeUInt8(0)
0x3A:
@@ -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(parseFloat(parameter.value))
+ else
+ buffer.writeUInt8(0)
+ ###
0x3C:
type: 'MONEY'
name: 'Money'
@@ -130,13 +150,43 @@ 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(parseFloat(parameter.value))
+ else
+ buffer.writeUInt8(0)
0x7A:
type: 'MONEY4'
name: 'SmallMoney'
0x7F:
type: 'INT8'
name: 'BigInt'
+ declaration: (parameter) ->
+ 'bigint'
+ writeParameterData: (buffer, parameter) ->
+ # ParamMetaData (TYPE_INFO)
+ buffer.writeUInt8(typeByName.IntN.id)
+ buffer.writeUInt8(8)
+ # ParamLenData
+ if parameter.value
+ buffer.writeUInt8(8)
+ if parseInt(parameter.value) > 0x100000000 # 4294967296
+ buffer.writeUInt32LE(parseInt(parameter.value) % 0x100000000)
+ else
+ buffer.writeInt32LE(parseInt(parameter.value) % 0x100000000)
+ buffer.writeInt32LE(Math.floor(parseInt(parameter.value) / 0x100000000))
+ else
+ buffer.writeUInt8(0)
+
# Variable-length types
0x22:
type: 'IMAGE'
@@ -155,6 +205,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'
@@ -208,7 +271,7 @@ TYPE =
if parameter.length
length = parameter.length
else if parameter.value?
- length = parameter.value.length
+ length = parameter.value.toString().length
else
length = @.maximumLength
@@ -220,7 +283,7 @@ TYPE =
if parameter.length
length = parameter.length
else if parameter.value?
- length = parameter.value.length
+ length = parameter.value.toString().length
else
length = @.maximumLength
@@ -236,13 +299,13 @@ TYPE =
if parameter.value?
if length <= @maximumLength
buffer.writeUInt16LE(length)
- buffer.writeString(parameter.value, 'ascii')
+ buffer.writeString(parameter.value.toString(), 'ascii')
else
# Length of all chunks.
buffer.writeUInt64LE(length)
# One chunk.
buffer.writeUInt32LE(length)
- buffer.writeString(parameter.value, 'ascii')
+ buffer.writeString(parameter.value.toString(), 'ascii')
# PLP_TERMINATOR (no more chunks).
buffer.writeUInt32LE(0)
else
@@ -266,7 +329,7 @@ TYPE =
if parameter.length
length = 2 * parameter.length
else if parameter.value?
- length = 2 * parameter.value.length
+ length = 2 * parameter.value.toString().length
else
length = @maximumLength
@@ -278,7 +341,7 @@ TYPE =
if parameter.length
length = 2 * parameter.length
else if parameter.value?
- length = 2 * parameter.value.length
+ length = 2 * parameter.value.toString().length
else
length = @maximumLength
@@ -294,13 +357,13 @@ TYPE =
if parameter.value?
if length <= @maximumLength
buffer.writeUInt16LE(length)
- buffer.writeString(parameter.value, 'ucs2')
+ buffer.writeString(parameter.value.toString(), 'ucs2')
else
# Length of all chunks.
buffer.writeUInt64LE(length)
# One chunk.
buffer.writeUInt32LE(length)
- buffer.writeString(parameter.value, 'ucs2')
+ buffer.writeString(parameter.value.toString(), 'ucs2')
# PLP_TERMINATOR (no more chunks).
buffer.writeUInt32LE(0)
else
View
@@ -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
@@ -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
@@ -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 = ->
@@ -44,6 +45,24 @@ exports.smallIntNull = (test) ->
exports.int = (test) ->
execSql(test, TYPES.Int, 8)
+
+exports.bigint = (test) ->
+ execSql(test, TYPES.BigInt, 9007199254740992)
+
+exports.bigint1 = (test) ->
+ execSql(test, TYPES.BigInt, 1)
+
+exports.bigintsmall = (test) ->
+ execSql(test, TYPES.BigInt, -9007199254740992)
+
+exports.bigintsmall1 = (test) ->
+ execSql(test, TYPES.BigInt, -1)
+
+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)
@@ -118,6 +137,24 @@ exports.outputSmallIntNull = (test) ->
exports.outputInt = (test) ->
execSqlOutput(test, TYPES.Int, 3)
+
+exports.outputBigInt = (test) ->
+ execSqlOutput(test, TYPES.BigInt, 9007199254740992)
+
+exports.outputBigInt1 = (test) ->
+ execSqlOutput(test, TYPES.BigInt, 1)
+
+exports.outputBigIntSmall = (test) ->
+ execSqlOutput(test, TYPES.BigInt, -9007199254740992)
+
+exports.outputBigIntSmall1 = (test) ->
+ execSqlOutput(test, TYPES.BigInt, -1)
+
+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)
@@ -209,6 +246,10 @@ 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())
+ else if (type == TYPES.UniqueIdentifierN)
+ test.deepEqual(columns[0].value, guidParser.guidToArray(value))
else
test.strictEqual(columns[0].value, value)
)
@@ -255,6 +296,10 @@ execSqlOutput = (test, type, value) ->
if (value instanceof Date)
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)

0 comments on commit 2a498fb

Please sign in to comment.