Permalink
Browse files

Fix PLP body in VarChar/NVarChar/VarBinary

PLP body length was wrongly determined by scale - it must be determined
by data length, scale might not be equal to data length.
  • Loading branch information...
1 parent 342315e commit 97005e85f703c11c500b28c35e8f6a231d41981c @patriksimek patriksimek committed Mar 31, 2014
View
@@ -340,16 +340,10 @@ TYPE =
# ParamLenData (TYPE_VARBYTE)
if parameter.value?
if parameter.length <= @maximumLength
- buffer.writeUInt16LE parameter.length
- buffer.writeBuffer parameter.value
+ buffer.writeUsVarbyte parameter.value
else
- # Length of all chunks.
- buffer.writeUInt64LE parameter.length
- # One chunk.
- buffer.writeUInt32LE parameter.length
- buffer.writeBuffer parameter.value
- # PLP_TERMINATOR (no more chunks).
- buffer.writeUInt32LE 0
+ # PLP_BODY
+ buffer.writePLPBody parameter.value
else
if parameter.length <= @maximumLength
buffer.writeUInt16LE NULL
@@ -398,22 +392,10 @@ TYPE =
# ParamLenData (TYPE_VARBYTE)
if parameter.value?
if parameter.length <= @maximumLength
- if Buffer.isBuffer parameter.value
- buffer.writeUInt16LE parameter.length
- buffer.writeBuffer parameter.value
- else
- buffer.writeUsVarbyte parameter.value.toString(), 'ascii'
+ buffer.writeUsVarbyte parameter.value, 'ascii'
else
- # Length of all chunks.
- buffer.writeUInt64LE(parameter.length)
- # One chunk.
- if Buffer.isBuffer parameter.value
- buffer.writeUInt32LE parameter.length
- buffer.writeBuffer parameter.value
- else
- buffer.writeLVarbyte parameter.value.toString(), 'ascii'
- # PLP_TERMINATOR (no more chunks).
- buffer.writeUInt32LE(0)
+ # PLP_BODY
+ buffer.writePLPBody parameter.value, 'ascii'
else
if parameter.length <= @maximumLength
buffer.writeUInt16LE(NULL)
@@ -490,22 +472,10 @@ TYPE =
# ParamLenData (TYPE_VARBYTE)
if parameter.value?
if parameter.length <= @maximumLength
- if Buffer.isBuffer parameter.value
- buffer.writeUInt16LE parameter.length * 2
- buffer.writeBuffer parameter.value
- else
- buffer.writeUsVarbyte parameter.value.toString(), 'ucs2'
+ buffer.writeUsVarbyte parameter.value, 'ucs2'
else
- # Length of all chunks.
- buffer.writeUInt64LE parameter.length * 2
- # One chunk.
- if Buffer.isBuffer parameter.value
- buffer.writeUInt32LE parameter.length * 2
- buffer.writeBuffer parameter.value
- else
- buffer.writeLVarbyte parameter.value.toString(), 'ucs2'
- # PLP_TERMINATOR (no more chunks).
- buffer.writeUInt32LE(0)
+ # PLP_BODY
+ buffer.writePLPBody parameter.value, 'ucs2'
else
if parameter.length <= @maximumLength
buffer.writeUInt16LE(NULL)
@@ -152,22 +152,43 @@ class WritableTrackingBuffer
@writeString(value, encoding)
writeUsVarbyte: (value, encoding = @encoding) ->
- length = Buffer.byteLength(value, encoding)
- @writeUInt16LE(length)
- @makeRoomFor(length)
- bytesWritten = @buffer.write(value, @position, encoding)
- @position += length
+ if Buffer.isBuffer value
+ length = value.length
+ else
+ value = value.toString()
+ length = Buffer.byteLength value, encoding
+
+ @writeUInt16LE length
+
+ if Buffer.isBuffer value
+ @writeBuffer value
+ else
+ @makeRoomFor length
+ @buffer.write(value, @position, encoding)
+ @position += length
- bytesWritten
-
- writeLVarbyte: (value, encoding = @encoding) ->
- length = Buffer.byteLength(value, encoding)
- @writeUInt32LE(length)
- @makeRoomFor(length)
- bytesWritten = @buffer.write(value, @position, encoding)
- @position += length
+ writePLPBody: (value, encoding = @encoding) ->
+ if Buffer.isBuffer value
+ length = value.length
+ else
+ value = value.toString()
+ length = Buffer.byteLength value, encoding
+
+ # Length of all chunks.
+ @writeUInt64LE length
+
+ # One chunk.
+ @writeUInt32LE length
+
+ if Buffer.isBuffer value
+ @writeBuffer value
+ else
+ @makeRoomFor length
+ @buffer.write value, @position, encoding
+ @position += length
- bytesWritten
+ # PLP_TERMINATOR (no more chunks).
+ @writeUInt32LE(0)
writeBuffer: (value) ->
length = value.length
@@ -79,6 +79,9 @@ exports.intNull = (test) ->
exports.varChar = (test) ->
execSql(test, TYPES.VarChar, 'qaz')
+exports.varCharN = (test) ->
+ execSql(test, TYPES.VarChar, 'qaz', null, {length: 8001})
+
exports.varCharEmptyString = (test) ->
execSql(test, TYPES.VarChar, '')
@@ -95,6 +98,9 @@ exports.varCharMax = (test) ->
exports.nVarChar = (test) ->
execSql(test, TYPES.NVarChar, 'qaz')
+exports.nVarCharN = (test) ->
+ execSql(test, TYPES.NVarChar, 'qaz', null, {length: 4001})
+
exports.nVarCharEmptyString = (test) ->
execSql(test, TYPES.NVarChar, '')
@@ -245,7 +251,7 @@ exports.multipleParameters = (test) ->
#console.log(text)
)
-execSql = (test, type, value, tdsVersion) ->
+execSql = (test, type, value, tdsVersion, options) ->
config = getConfig()
#config.options.packetSize = 32768
@@ -260,7 +266,7 @@ execSql = (test, type, value, tdsVersion) ->
connection.close()
)
- request.addParameter('param', type, value)
+ request.addParameter('param', type, value, options)
request.on('doneInProc', (rowCount, more) ->
test.ok(more)

0 comments on commit 97005e8

Please sign in to comment.