metadata-parser.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.readCollation = readCollation;
  6. exports.default = void 0;
  7. var _collation = require("./collation");
  8. var _dataType = require("./data-type");
  9. var _sprintfJs = require("sprintf-js");
  10. function readCollation(parser, callback) {
  11. // s2.2.5.1.2
  12. parser.readBuffer(5, collationData => {
  13. let lcid = (collationData[2] & 0x0F) << 16;
  14. lcid |= collationData[1] << 8;
  15. lcid |= collationData[0]; // This may not be extracting the correct nibbles in the correct order.
  16. let flags = collationData[3] >> 4;
  17. flags |= collationData[2] & 0xF0; // This may not be extracting the correct nibble.
  18. const version = collationData[3] & 0x0F;
  19. const sortId = collationData[4];
  20. const codepage = _collation.codepageBySortId[sortId] || _collation.codepageByLcid[lcid] || 'CP1252';
  21. callback({
  22. lcid,
  23. flags,
  24. version,
  25. sortId,
  26. codepage
  27. });
  28. });
  29. }
  30. function readSchema(parser, callback) {
  31. // s2.2.5.5.3
  32. parser.readUInt8(schemaPresent => {
  33. if (schemaPresent === 0x01) {
  34. parser.readBVarChar(dbname => {
  35. parser.readBVarChar(owningSchema => {
  36. parser.readUsVarChar(xmlSchemaCollection => {
  37. callback({
  38. dbname: dbname,
  39. owningSchema: owningSchema,
  40. xmlSchemaCollection: xmlSchemaCollection
  41. });
  42. });
  43. });
  44. });
  45. } else {
  46. callback(undefined);
  47. }
  48. });
  49. }
  50. function readUDTInfo(parser, callback) {
  51. parser.readUInt16LE(maxByteSize => {
  52. parser.readBVarChar(dbname => {
  53. parser.readBVarChar(owningSchema => {
  54. parser.readBVarChar(typeName => {
  55. parser.readUsVarChar(assemblyName => {
  56. callback({
  57. maxByteSize: maxByteSize,
  58. dbname: dbname,
  59. owningSchema: owningSchema,
  60. typeName: typeName,
  61. assemblyName: assemblyName
  62. });
  63. });
  64. });
  65. });
  66. });
  67. });
  68. }
  69. function metadataParse(parser, options, callback) {
  70. (options.tdsVersion < '7_2' ? parser.readUInt16LE : parser.readUInt32LE).call(parser, userType => {
  71. parser.readUInt16LE(flags => {
  72. parser.readUInt8(typeNumber => {
  73. const type = _dataType.TYPE[typeNumber];
  74. if (!type) {
  75. return parser.emit('error', new Error((0, _sprintfJs.sprintf)('Unrecognised data type 0x%02X', typeNumber)));
  76. }
  77. switch (type.name) {
  78. case 'Null':
  79. case 'TinyInt':
  80. case 'SmallInt':
  81. case 'Int':
  82. case 'BigInt':
  83. case 'Real':
  84. case 'Float':
  85. case 'SmallMoney':
  86. case 'Money':
  87. case 'Bit':
  88. case 'SmallDateTime':
  89. case 'DateTime':
  90. case 'Date':
  91. return callback({
  92. userType: userType,
  93. flags: flags,
  94. type: type,
  95. collation: undefined,
  96. precision: undefined,
  97. scale: undefined,
  98. dataLength: undefined,
  99. schema: undefined,
  100. udtInfo: undefined
  101. });
  102. case 'IntN':
  103. case 'FloatN':
  104. case 'MoneyN':
  105. case 'BitN':
  106. case 'UniqueIdentifier':
  107. case 'DateTimeN':
  108. return parser.readUInt8(dataLength => {
  109. callback({
  110. userType: userType,
  111. flags: flags,
  112. type: type,
  113. collation: undefined,
  114. precision: undefined,
  115. scale: undefined,
  116. dataLength: dataLength,
  117. schema: undefined,
  118. udtInfo: undefined
  119. });
  120. });
  121. case 'Variant':
  122. return parser.readUInt32LE(dataLength => {
  123. callback({
  124. userType: userType,
  125. flags: flags,
  126. type: type,
  127. collation: undefined,
  128. precision: undefined,
  129. scale: undefined,
  130. dataLength: dataLength,
  131. schema: undefined,
  132. udtInfo: undefined
  133. });
  134. });
  135. case 'VarChar':
  136. case 'Char':
  137. case 'NVarChar':
  138. case 'NChar':
  139. return parser.readUInt16LE(dataLength => {
  140. readCollation(parser, collation => {
  141. callback({
  142. userType: userType,
  143. flags: flags,
  144. type: type,
  145. collation: collation,
  146. precision: undefined,
  147. scale: undefined,
  148. dataLength: dataLength,
  149. schema: undefined,
  150. udtInfo: undefined
  151. });
  152. });
  153. });
  154. case 'Text':
  155. case 'NText':
  156. return parser.readUInt32LE(dataLength => {
  157. readCollation(parser, collation => {
  158. callback({
  159. userType: userType,
  160. flags: flags,
  161. type: type,
  162. collation: collation,
  163. precision: undefined,
  164. scale: undefined,
  165. dataLength: dataLength,
  166. schema: undefined,
  167. udtInfo: undefined
  168. });
  169. });
  170. });
  171. case 'VarBinary':
  172. case 'Binary':
  173. return parser.readUInt16LE(dataLength => {
  174. callback({
  175. userType: userType,
  176. flags: flags,
  177. type: type,
  178. collation: undefined,
  179. precision: undefined,
  180. scale: undefined,
  181. dataLength: dataLength,
  182. schema: undefined,
  183. udtInfo: undefined
  184. });
  185. });
  186. case 'Image':
  187. return parser.readUInt32LE(dataLength => {
  188. callback({
  189. userType: userType,
  190. flags: flags,
  191. type: type,
  192. collation: undefined,
  193. precision: undefined,
  194. scale: undefined,
  195. dataLength: dataLength,
  196. schema: undefined,
  197. udtInfo: undefined
  198. });
  199. });
  200. case 'Xml':
  201. return readSchema(parser, schema => {
  202. callback({
  203. userType: userType,
  204. flags: flags,
  205. type: type,
  206. collation: undefined,
  207. precision: undefined,
  208. scale: undefined,
  209. dataLength: undefined,
  210. schema: schema,
  211. udtInfo: undefined
  212. });
  213. });
  214. case 'Time':
  215. case 'DateTime2':
  216. case 'DateTimeOffset':
  217. return parser.readUInt8(scale => {
  218. callback({
  219. userType: userType,
  220. flags: flags,
  221. type: type,
  222. collation: undefined,
  223. precision: undefined,
  224. scale: scale,
  225. dataLength: undefined,
  226. schema: undefined,
  227. udtInfo: undefined
  228. });
  229. });
  230. case 'NumericN':
  231. case 'DecimalN':
  232. return parser.readUInt8(dataLength => {
  233. parser.readUInt8(precision => {
  234. parser.readUInt8(scale => {
  235. callback({
  236. userType: userType,
  237. flags: flags,
  238. type: type,
  239. collation: undefined,
  240. precision: precision,
  241. scale: scale,
  242. dataLength: dataLength,
  243. schema: undefined,
  244. udtInfo: undefined
  245. });
  246. });
  247. });
  248. });
  249. case 'UDT':
  250. return readUDTInfo(parser, udtInfo => {
  251. callback({
  252. userType: userType,
  253. flags: flags,
  254. type: type,
  255. collation: undefined,
  256. precision: undefined,
  257. scale: undefined,
  258. dataLength: undefined,
  259. schema: undefined,
  260. udtInfo: udtInfo
  261. });
  262. });
  263. default:
  264. return parser.emit('error', new Error((0, _sprintfJs.sprintf)('Unrecognised type %s', type.name)));
  265. }
  266. });
  267. });
  268. });
  269. }
  270. var _default = metadataParse;
  271. exports.default = _default;
  272. module.exports = metadataParse;
  273. module.exports.readCollation = readCollation;