/
metadata-parser.coffee
87 lines (70 loc) · 2.17 KB
/
metadata-parser.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
codepageByLcid = require('./collation').codepageByLcid
TYPE = require('./data-type').TYPE
sprintf = require('sprintf').sprintf
parse = (buffer, tdsVersion) ->
if tdsVersion < "7_2"
userType = buffer.readUInt16LE()
else
userType = buffer.readUInt32LE()
flags = buffer.readUInt16LE()
typeNumber = buffer.readUInt8()
type = TYPE[typeNumber]
if !type
throw new Error(sprintf('Unrecognised data type 0x%02X at offset 0x%04X', typeNumber, (buffer.position - 1)))
#console.log(type)
if (type.id & 0x30) == 0x20
# xx10xxxx - s2.2.4.2.1.3
# Variable length
switch type.dataLengthLength
when 1
dataLength = buffer.readUInt8()
when 2
dataLength = buffer.readUInt16LE()
when 4
dataLength = buffer.readUInt32LE()
else
throw new Error("Unsupported dataLengthLength #{type.dataLengthLength} for data type #{type.name}")
else
dataLength = undefined
if type.hasPrecision
precision = buffer.readUInt8()
else
precision = undefined
if type.hasScale
scale = buffer.readUInt8()
else
scale = undefined
if type.hasCollation
# s2.2.5.1.2
collationData = buffer.readBuffer(5)
collation = {}
collation.lcid = (collationData[2] & 0x0F) << 16
collation.lcid |= collationData[1] << 8
collation.lcid |= collationData[0]
collation.codepage = codepageByLcid[collation.lcid]
# This may not be extracting the correct nibbles in the correct order.
collation.flags = collationData[3] >> 4
collation.flags |= collationData[2] & 0xF0
# This may not be extracting the correct nibble.
collation.version = collationData[3] & 0x0F
collation.sortId = collationData[4]
else
collation = undefined
schema = undefined
if type.hasSchemaPresent
schemaPresent = buffer.readUInt8()
if schemaPresent == 0x01
schema =
dbname: buffer.readBVarchar()
owningSchema: buffer.readBVarchar()
xmlSchemaCollection: buffer.readUsVarchar()
metadata =
userType: userType
flags: flags
type: type
collation: collation
precision: precision
scale: scale
dataLength: dataLength
schema: schema
module.exports = parse