RowDataPacket.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var Types = require('../constants/types');
  2. var Charsets = require('../constants/charsets');
  3. var Field = require('./Field');
  4. var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
  5. module.exports = RowDataPacket;
  6. function RowDataPacket() {
  7. }
  8. Object.defineProperty(RowDataPacket.prototype, 'parse', {
  9. configurable : true,
  10. enumerable : false,
  11. value : parse
  12. });
  13. Object.defineProperty(RowDataPacket.prototype, '_typeCast', {
  14. configurable : true,
  15. enumerable : false,
  16. value : typeCast
  17. });
  18. function parse(parser, fieldPackets, typeCast, nestTables, connection) {
  19. var self = this;
  20. var next = function () {
  21. return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
  22. };
  23. for (var i = 0; i < fieldPackets.length; i++) {
  24. var fieldPacket = fieldPackets[i];
  25. var value;
  26. if (typeof typeCast === 'function') {
  27. value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
  28. } else {
  29. value = (typeCast)
  30. ? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
  31. : ( (fieldPacket.charsetNr === Charsets.BINARY)
  32. ? parser.parseLengthCodedBuffer()
  33. : parser.parseLengthCodedString() );
  34. }
  35. if (typeof nestTables === 'string' && nestTables.length) {
  36. this[fieldPacket.table + nestTables + fieldPacket.name] = value;
  37. } else if (nestTables) {
  38. this[fieldPacket.table] = this[fieldPacket.table] || {};
  39. this[fieldPacket.table][fieldPacket.name] = value;
  40. } else {
  41. this[fieldPacket.name] = value;
  42. }
  43. }
  44. }
  45. function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings, dateStrings) {
  46. var numberString;
  47. switch (field.type) {
  48. case Types.TIMESTAMP:
  49. case Types.TIMESTAMP2:
  50. case Types.DATE:
  51. case Types.DATETIME:
  52. case Types.DATETIME2:
  53. case Types.NEWDATE:
  54. var dateString = parser.parseLengthCodedString();
  55. if (typeMatch(field.type, dateStrings)) {
  56. return dateString;
  57. }
  58. if (dateString === null) {
  59. return null;
  60. }
  61. var originalString = dateString;
  62. if (field.type === Types.DATE) {
  63. dateString += ' 00:00:00';
  64. }
  65. if (timeZone !== 'local') {
  66. dateString += ' ' + timeZone;
  67. }
  68. var dt = new Date(dateString);
  69. if (isNaN(dt.getTime())) {
  70. return originalString;
  71. }
  72. return dt;
  73. case Types.TINY:
  74. case Types.SHORT:
  75. case Types.LONG:
  76. case Types.INT24:
  77. case Types.YEAR:
  78. case Types.FLOAT:
  79. case Types.DOUBLE:
  80. numberString = parser.parseLengthCodedString();
  81. return (numberString === null || (field.zeroFill && numberString[0] === '0'))
  82. ? numberString : Number(numberString);
  83. case Types.NEWDECIMAL:
  84. case Types.LONGLONG:
  85. numberString = parser.parseLengthCodedString();
  86. return (numberString === null || (field.zeroFill && numberString[0] === '0'))
  87. ? numberString
  88. : ((supportBigNumbers && (bigNumberStrings || (Number(numberString) >= IEEE_754_BINARY_64_PRECISION) || Number(numberString) <= -IEEE_754_BINARY_64_PRECISION))
  89. ? numberString
  90. : Number(numberString));
  91. case Types.BIT:
  92. return parser.parseLengthCodedBuffer();
  93. case Types.STRING:
  94. case Types.VAR_STRING:
  95. case Types.TINY_BLOB:
  96. case Types.MEDIUM_BLOB:
  97. case Types.LONG_BLOB:
  98. case Types.BLOB:
  99. return (field.charsetNr === Charsets.BINARY)
  100. ? parser.parseLengthCodedBuffer()
  101. : parser.parseLengthCodedString();
  102. case Types.GEOMETRY:
  103. return parser.parseGeometryValue();
  104. default:
  105. return parser.parseLengthCodedString();
  106. }
  107. }
  108. function typeMatch(type, list) {
  109. if (Array.isArray(list)) {
  110. return list.indexOf(Types[type]) !== -1;
  111. } else {
  112. return Boolean(list);
  113. }
  114. }