RowDataPacket.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTables, connection) {
  9. var self = this;
  10. var next = function () {
  11. return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers);
  12. };
  13. for (var i = 0; i < fieldPackets.length; i++) {
  14. var fieldPacket = fieldPackets[i];
  15. var value;
  16. if (typeof typeCast == "function") {
  17. value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
  18. } else {
  19. value = (typeCast)
  20. ? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers)
  21. : ( (fieldPacket.charsetNr === Charsets.BINARY)
  22. ? parser.parseLengthCodedBuffer()
  23. : parser.parseLengthCodedString() );
  24. }
  25. if (typeof nestTables == "string" && nestTables.length) {
  26. this[fieldPacket.table + nestTables + fieldPacket.name] = value;
  27. } else if (nestTables) {
  28. this[fieldPacket.table] = this[fieldPacket.table] || {};
  29. this[fieldPacket.table][fieldPacket.name] = value;
  30. } else {
  31. this[fieldPacket.name] = value;
  32. }
  33. }
  34. };
  35. RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBigNumbers) {
  36. switch (field.type) {
  37. case Types.TIMESTAMP:
  38. case Types.DATE:
  39. case Types.DATETIME:
  40. case Types.NEWDATE:
  41. var dateString = parser.parseLengthCodedString();
  42. if (dateString === null) {
  43. return null;
  44. }
  45. if (timeZone != 'local') {
  46. if (field.type === Types.DATE) {
  47. dateString += ' 00:00:00 ' + timeZone;
  48. } else {
  49. dateString += timeZone;
  50. }
  51. }
  52. return new Date(dateString);
  53. case Types.TINY:
  54. case Types.SHORT:
  55. case Types.LONG:
  56. case Types.INT24:
  57. case Types.YEAR:
  58. case Types.FLOAT:
  59. case Types.DOUBLE:
  60. case Types.LONGLONG:
  61. case Types.NEWDECIMAL:
  62. var numberString = parser.parseLengthCodedString();
  63. return (numberString === null || (field.zeroFill && numberString[0] == "0"))
  64. ? numberString
  65. : ((supportBigNumbers && Number(numberString) > IEEE_754_BINARY_64_PRECISION)
  66. ? numberString
  67. : Number(numberString));
  68. case Types.BIT:
  69. return parser.parseLengthCodedBuffer();
  70. case Types.STRING:
  71. case Types.VAR_STRING:
  72. case Types.TINY_BLOB:
  73. case Types.MEDIUM_BLOB:
  74. case Types.LONG_BLOB:
  75. case Types.BLOB:
  76. return (field.charsetNr === Charsets.BINARY)
  77. ? parser.parseLengthCodedBuffer()
  78. : parser.parseLengthCodedString();
  79. case Types.GEOMETRY:
  80. return parser.parseGeometryValue();
  81. default:
  82. return parser.parseLengthCodedString();
  83. }
  84. };