forked from cretz/node-tds
/
envchange.token.coffee
43 lines (37 loc) · 1.28 KB
/
envchange.token.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{TdsConstants} = require './tds-constants'
{Token} = require './token'
###*
Token for ENVCHANGE (0xE3)
@spec 2.2.7.8
###
class exports.EnvChangeToken extends Token
@type: 0xE3
@name: 'ENVCHANGE'
constructor: ->
@type = 0xE3
@name = 'ENVCHANGE'
@handlerFunction = 'envchange'
_readValue: (typedef, stream, context) ->
if not typedef?
stream.skip 1
null
else if typedef is '2byteskip'
stream.skip 2
null
else
switch typedef
when 'string' then stream.readUcs2String stream.readByte()
when 'bytes' then stream.readBuffer stream.readByte()
when 'byte' then stream.readByte()
when 'longbytes' then stream.readBuffer stream.readUInt32LE()
when 'shortbytes' then stream.readBuffer stream.readUInt16LE()
else throw new Error 'Unrecognized typedef: ' + typedef
fromBuffer: (stream, context) ->
@length = stream.readUInt16LE()
@changeType = stream.readByte()
stream.assertBytesAvailable @length
typedef = TdsConstants.envChangeTypesByNumber[@changeType]
if not typedef?
throw new Error 'Unrecognized envchange type: ' + @changeType
@newValue = @_readValue typedef.newValue, stream, context
@oldValue = @_readValue typedef.oldValue, stream, context