binary_parser.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. const FieldFlags = require('../constants/field_flags.js');
  3. const Charsets = require('../constants/charsets.js');
  4. const Types = require('../constants/types.js');
  5. const helpers = require('../helpers');
  6. const genFunc = require('generate-function');
  7. const parserCache = require('./parser_cache.js');
  8. const typeNames = [];
  9. for (const t in Types) {
  10. typeNames[Types[t]] = t;
  11. }
  12. function readCodeFor(field, config, options, fieldNum) {
  13. const supportBigNumbers = Boolean(
  14. options.supportBigNumbers || config.supportBigNumbers,
  15. );
  16. const bigNumberStrings = Boolean(
  17. options.bigNumberStrings || config.bigNumberStrings,
  18. );
  19. const timezone = options.timezone || config.timezone;
  20. const dateStrings = options.dateStrings || config.dateStrings;
  21. const unsigned = field.flags & FieldFlags.UNSIGNED;
  22. switch (field.columnType) {
  23. case Types.TINY:
  24. return unsigned ? 'packet.readInt8();' : 'packet.readSInt8();';
  25. case Types.SHORT:
  26. return unsigned ? 'packet.readInt16();' : 'packet.readSInt16();';
  27. case Types.LONG:
  28. case Types.INT24: // in binary protocol int24 is encoded in 4 bytes int32
  29. return unsigned ? 'packet.readInt32();' : 'packet.readSInt32();';
  30. case Types.YEAR:
  31. return 'packet.readInt16()';
  32. case Types.FLOAT:
  33. return 'packet.readFloat();';
  34. case Types.DOUBLE:
  35. return 'packet.readDouble();';
  36. case Types.NULL:
  37. return 'null;';
  38. case Types.DATE:
  39. case Types.DATETIME:
  40. case Types.TIMESTAMP:
  41. case Types.NEWDATE:
  42. if (helpers.typeMatch(field.columnType, dateStrings, Types)) {
  43. return `packet.readDateTimeString(${parseInt(field.decimals, 10)});`;
  44. }
  45. return `packet.readDateTime(${helpers.srcEscape(timezone)});`;
  46. case Types.TIME:
  47. return 'packet.readTimeString()';
  48. case Types.DECIMAL:
  49. case Types.NEWDECIMAL:
  50. if (config.decimalNumbers) {
  51. return 'packet.parseLengthCodedFloat();';
  52. }
  53. return 'packet.readLengthCodedString("ascii");';
  54. case Types.GEOMETRY:
  55. return 'packet.parseGeometryValue();';
  56. case Types.VECTOR:
  57. return 'packet.parseVector()';
  58. case Types.JSON:
  59. // Since for JSON columns mysql always returns charset 63 (BINARY),
  60. // we have to handle it according to JSON specs and use "utf8",
  61. // see https://github.com/sidorares/node-mysql2/issues/409
  62. return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"));';
  63. case Types.LONGLONG:
  64. if (!supportBigNumbers) {
  65. return unsigned
  66. ? 'packet.readInt64JSNumber();'
  67. : 'packet.readSInt64JSNumber();';
  68. }
  69. if (bigNumberStrings) {
  70. return unsigned
  71. ? 'packet.readInt64String();'
  72. : 'packet.readSInt64String();';
  73. }
  74. return unsigned ? 'packet.readInt64();' : 'packet.readSInt64();';
  75. default:
  76. if (field.characterSet === Charsets.BINARY) {
  77. return 'packet.readLengthCodedBuffer();';
  78. }
  79. return `packet.readLengthCodedString(fields[${fieldNum}].encoding)`;
  80. }
  81. }
  82. function compile(fields, options, config) {
  83. const parserFn = genFunc();
  84. const nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
  85. function wrap(field, packet) {
  86. return {
  87. type: typeNames[field.columnType],
  88. length: field.columnLength,
  89. db: field.schema,
  90. table: field.table,
  91. name: field.name,
  92. string: function (encoding = field.encoding) {
  93. if (field.columnType === Types.JSON && encoding === field.encoding) {
  94. // Since for JSON columns mysql always returns charset 63 (BINARY),
  95. // we have to handle it according to JSON specs and use "utf8",
  96. // see https://github.com/sidorares/node-mysql2/issues/1661
  97. console.warn(
  98. `typeCast: JSON column "${field.name}" is interpreted as BINARY by default, recommended to manually set utf8 encoding: \`field.string("utf8")\``,
  99. );
  100. }
  101. if (
  102. [Types.DATETIME, Types.NEWDATE, Types.TIMESTAMP, Types.DATE].includes(
  103. field.columnType,
  104. )
  105. ) {
  106. return packet.readDateTimeString(parseInt(field.decimals, 10));
  107. }
  108. if (field.columnType === Types.TINY) {
  109. const unsigned = field.flags & FieldFlags.UNSIGNED;
  110. return String(unsigned ? packet.readInt8() : packet.readSInt8());
  111. }
  112. if (field.columnType === Types.TIME) {
  113. return packet.readTimeString();
  114. }
  115. return packet.readLengthCodedString(encoding);
  116. },
  117. buffer: function () {
  118. return packet.readLengthCodedBuffer();
  119. },
  120. geometry: function () {
  121. return packet.parseGeometryValue();
  122. },
  123. };
  124. }
  125. parserFn('(function(){');
  126. parserFn('return class BinaryRow {');
  127. parserFn('constructor() {');
  128. parserFn('}');
  129. parserFn('next(packet, fields, options) {');
  130. if (options.rowsAsArray) {
  131. parserFn(`const result = new Array(${fields.length});`);
  132. } else {
  133. parserFn('const result = {};');
  134. }
  135. // Global typeCast
  136. if (
  137. typeof config.typeCast === 'function' &&
  138. typeof options.typeCast !== 'function'
  139. ) {
  140. options.typeCast = config.typeCast;
  141. }
  142. parserFn('packet.readInt8();'); // status byte
  143. for (let i = 0; i < nullBitmapLength; ++i) {
  144. parserFn(`const nullBitmaskByte${i} = packet.readInt8();`);
  145. }
  146. let lvalue = '';
  147. let currentFieldNullBit = 4;
  148. let nullByteIndex = 0;
  149. let fieldName = '';
  150. let tableName = '';
  151. for (let i = 0; i < fields.length; i++) {
  152. fieldName = helpers.fieldEscape(fields[i].name);
  153. // parserFn(`// ${fieldName}: ${typeNames[fields[i].columnType]}`);
  154. if (typeof options.nestTables === 'string') {
  155. lvalue = `result[${helpers.fieldEscape(fields[i].table + options.nestTables + fields[i].name)}]`;
  156. } else if (options.nestTables === true) {
  157. tableName = helpers.fieldEscape(fields[i].table);
  158. parserFn(`if (!result[${tableName}]) result[${tableName}] = {};`);
  159. lvalue = `result[${tableName}][${fieldName}]`;
  160. } else if (options.rowsAsArray) {
  161. lvalue = `result[${i.toString(10)}]`;
  162. } else {
  163. lvalue = `result[${fieldName}]`;
  164. }
  165. parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) `);
  166. parserFn(`${lvalue} = null;`);
  167. parserFn('else {');
  168. if (options.typeCast === false) {
  169. parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
  170. } else {
  171. const fieldWrapperVar = `fieldWrapper${i}`;
  172. parserFn(`const ${fieldWrapperVar} = wrap(fields[${i}], packet);`);
  173. const readCode = readCodeFor(fields[i], config, options, i);
  174. if (typeof options.typeCast === 'function') {
  175. parserFn(
  176. `${lvalue} = options.typeCast(${fieldWrapperVar}, function() { return ${readCode} });`,
  177. );
  178. } else {
  179. parserFn(`${lvalue} = ${readCode};`);
  180. }
  181. }
  182. parserFn('}');
  183. currentFieldNullBit *= 2;
  184. if (currentFieldNullBit === 0x100) {
  185. currentFieldNullBit = 1;
  186. nullByteIndex++;
  187. }
  188. }
  189. parserFn('return result;');
  190. parserFn('}');
  191. parserFn('};')('})()');
  192. if (config.debug) {
  193. helpers.printDebugWithCode(
  194. 'Compiled binary protocol row parser',
  195. parserFn.toString(),
  196. );
  197. }
  198. return parserFn.toFunction({ wrap });
  199. }
  200. function getBinaryParser(fields, options, config) {
  201. return parserCache.getParser('binary', fields, options, config, compile);
  202. }
  203. module.exports = getBinaryParser;