text_parser.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. const Types = require('../constants/types.js');
  3. const Charsets = require('../constants/charsets.js');
  4. const helpers = require('../helpers');
  5. const genFunc = require('generate-function');
  6. const parserCache = require('./parser_cache.js');
  7. const typeNames = [];
  8. for (const t in Types) {
  9. typeNames[Types[t]] = t;
  10. }
  11. function readCodeFor(type, charset, encodingExpr, config, options) {
  12. const supportBigNumbers = Boolean(
  13. options.supportBigNumbers || config.supportBigNumbers,
  14. );
  15. const bigNumberStrings = Boolean(
  16. options.bigNumberStrings || config.bigNumberStrings,
  17. );
  18. const timezone = options.timezone || config.timezone;
  19. const dateStrings = options.dateStrings || config.dateStrings;
  20. switch (type) {
  21. case Types.TINY:
  22. case Types.SHORT:
  23. case Types.LONG:
  24. case Types.INT24:
  25. case Types.YEAR:
  26. return 'packet.parseLengthCodedIntNoBigCheck()';
  27. case Types.LONGLONG:
  28. if (supportBigNumbers && bigNumberStrings) {
  29. return 'packet.parseLengthCodedIntString()';
  30. }
  31. return `packet.parseLengthCodedInt(${supportBigNumbers})`;
  32. case Types.FLOAT:
  33. case Types.DOUBLE:
  34. return 'packet.parseLengthCodedFloat()';
  35. case Types.NULL:
  36. return 'packet.readLengthCodedNumber()';
  37. case Types.DECIMAL:
  38. case Types.NEWDECIMAL:
  39. if (config.decimalNumbers) {
  40. return 'packet.parseLengthCodedFloat()';
  41. }
  42. return 'packet.readLengthCodedString("ascii")';
  43. case Types.DATE:
  44. if (helpers.typeMatch(type, dateStrings, Types)) {
  45. return 'packet.readLengthCodedString("ascii")';
  46. }
  47. return `packet.parseDate(${helpers.srcEscape(timezone)})`;
  48. case Types.DATETIME:
  49. case Types.TIMESTAMP:
  50. if (helpers.typeMatch(type, dateStrings, Types)) {
  51. return 'packet.readLengthCodedString("ascii")';
  52. }
  53. return `packet.parseDateTime(${helpers.srcEscape(timezone)})`;
  54. case Types.TIME:
  55. return 'packet.readLengthCodedString("ascii")';
  56. case Types.GEOMETRY:
  57. return 'packet.parseGeometryValue()';
  58. case Types.VECTOR:
  59. return 'packet.parseVector()';
  60. case Types.JSON:
  61. // Since for JSON columns mysql always returns charset 63 (BINARY),
  62. // we have to handle it according to JSON specs and use "utf8",
  63. // see https://github.com/sidorares/node-mysql2/issues/409
  64. return config.jsonStrings ? 'packet.readLengthCodedString("utf8")' : 'JSON.parse(packet.readLengthCodedString("utf8"))';
  65. default:
  66. if (charset === Charsets.BINARY) {
  67. return 'packet.readLengthCodedBuffer()';
  68. }
  69. return `packet.readLengthCodedString(${encodingExpr})`;
  70. }
  71. }
  72. function compile(fields, options, config) {
  73. // use global typeCast if current query doesn't specify one
  74. if (
  75. typeof config.typeCast === 'function' &&
  76. typeof options.typeCast !== 'function'
  77. ) {
  78. options.typeCast = config.typeCast;
  79. }
  80. function wrap(field, _this) {
  81. return {
  82. type: typeNames[field.columnType],
  83. length: field.columnLength,
  84. db: field.schema,
  85. table: field.table,
  86. name: field.name,
  87. string: function (encoding = field.encoding) {
  88. if (field.columnType === Types.JSON && encoding === field.encoding) {
  89. // Since for JSON columns mysql always returns charset 63 (BINARY),
  90. // we have to handle it according to JSON specs and use "utf8",
  91. // see https://github.com/sidorares/node-mysql2/issues/1661
  92. console.warn(
  93. `typeCast: JSON column "${field.name}" is interpreted as BINARY by default, recommended to manually set utf8 encoding: \`field.string("utf8")\``,
  94. );
  95. }
  96. return _this.packet.readLengthCodedString(encoding);
  97. },
  98. buffer: function () {
  99. return _this.packet.readLengthCodedBuffer();
  100. },
  101. geometry: function () {
  102. return _this.packet.parseGeometryValue();
  103. },
  104. };
  105. }
  106. const parserFn = genFunc();
  107. parserFn('(function () {')('return class TextRow {');
  108. // constructor method
  109. parserFn('constructor(fields) {');
  110. // node-mysql typeCast compatibility wrapper
  111. // see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
  112. if (typeof options.typeCast === 'function') {
  113. parserFn('const _this = this;');
  114. parserFn('for(let i=0; i<fields.length; ++i) {');
  115. parserFn('this[`wrap${i}`] = wrap(fields[i], _this);');
  116. parserFn('}');
  117. }
  118. parserFn('}');
  119. // next method
  120. parserFn('next(packet, fields, options) {');
  121. parserFn('this.packet = packet;');
  122. if (options.rowsAsArray) {
  123. parserFn(`const result = new Array(${fields.length});`);
  124. } else {
  125. parserFn('const result = {};');
  126. }
  127. const resultTables = {};
  128. let resultTablesArray = [];
  129. if (options.nestTables === true) {
  130. for (let i = 0; i < fields.length; i++) {
  131. resultTables[fields[i].table] = 1;
  132. }
  133. resultTablesArray = Object.keys(resultTables);
  134. for (let i = 0; i < resultTablesArray.length; i++) {
  135. parserFn(`result[${helpers.fieldEscape(resultTablesArray[i])}] = {};`);
  136. }
  137. }
  138. let lvalue = '';
  139. let fieldName = '';
  140. let tableName = '';
  141. for (let i = 0; i < fields.length; i++) {
  142. fieldName = helpers.fieldEscape(fields[i].name);
  143. // parserFn(`// ${fieldName}: ${typeNames[fields[i].columnType]}`);
  144. if (typeof options.nestTables === 'string') {
  145. lvalue = `result[${helpers.fieldEscape(fields[i].table + options.nestTables + fields[i].name)}]`;
  146. } else if (options.nestTables === true) {
  147. tableName = helpers.fieldEscape(fields[i].table);
  148. parserFn(`if (!result[${tableName}]) result[${tableName}] = {};`);
  149. lvalue = `result[${tableName}][${fieldName}]`;
  150. } else if (options.rowsAsArray) {
  151. lvalue = `result[${i.toString(10)}]`;
  152. } else {
  153. lvalue = `result[${fieldName}]`;
  154. }
  155. if (options.typeCast === false) {
  156. parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
  157. } else {
  158. const encodingExpr = `fields[${i}].encoding`;
  159. const readCode = readCodeFor(
  160. fields[i].columnType,
  161. fields[i].characterSet,
  162. encodingExpr,
  163. config,
  164. options,
  165. );
  166. if (typeof options.typeCast === 'function') {
  167. parserFn(
  168. `${lvalue} = options.typeCast(this.wrap${i}, function() { return ${readCode} });`,
  169. );
  170. } else {
  171. parserFn(`${lvalue} = ${readCode};`);
  172. }
  173. }
  174. }
  175. parserFn('return result;');
  176. parserFn('}');
  177. parserFn('};')('})()');
  178. if (config.debug) {
  179. helpers.printDebugWithCode(
  180. 'Compiled text protocol row parser',
  181. parserFn.toString(),
  182. );
  183. }
  184. if (typeof options.typeCast === 'function') {
  185. return parserFn.toFunction({ wrap });
  186. }
  187. return parserFn.toFunction();
  188. }
  189. function getTextParser(fields, options, config) {
  190. return parserCache.getParser('text', fields, options, config, compile);
  191. }
  192. module.exports = getTextParser;