binary_row.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const Types = require('../constants/types');
  3. const Packet = require('../packets/packet');
  4. const binaryReader = new Array(256);
  5. class BinaryRow {
  6. constructor(columns) {
  7. this.columns = columns || [];
  8. }
  9. static toPacket(columns, encoding) {
  10. // throw new Error('Not implemented');
  11. const sequenceId = 0; // TODO remove, this is calculated now in connecton
  12. let length = 0;
  13. columns.forEach(val => {
  14. if (val === null || typeof val === 'undefined') {
  15. ++length;
  16. return;
  17. }
  18. length += Packet.lengthCodedStringLength(val.toString(10), encoding);
  19. });
  20. length = length + 2;
  21. const buffer = Buffer.allocUnsafe(length + 4);
  22. const packet = new Packet(sequenceId, buffer, 0, length + 4);
  23. packet.offset = 4;
  24. packet.writeInt8(0);
  25. let bitmap = 0;
  26. let bitValue = 1;
  27. columns.forEach(parameter => {
  28. if (parameter.type === Types.NULL) {
  29. bitmap += bitValue;
  30. }
  31. bitValue *= 2;
  32. if (bitValue === 256) {
  33. packet.writeInt8(bitmap);
  34. bitmap = 0;
  35. bitValue = 1;
  36. }
  37. });
  38. if (bitValue !== 1) {
  39. packet.writeInt8(bitmap);
  40. }
  41. columns.forEach(val => {
  42. if (val === null) {
  43. packet.writeNull();
  44. return;
  45. }
  46. if (typeof val === 'undefined') {
  47. packet.writeInt8(0);
  48. return;
  49. }
  50. packet.writeLengthCodedString(val.toString(10), encoding);
  51. });
  52. return packet;
  53. }
  54. // TODO: complete list of types...
  55. static fromPacket(fields, packet) {
  56. const columns = new Array(fields.length);
  57. packet.readInt8(); // TODO check it's 0
  58. const nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
  59. // TODO: read and interpret null bitmap
  60. packet.skip(nullBitmapLength);
  61. for (let i = 0; i < columns.length; ++i) {
  62. columns[i] = binaryReader[fields[i].columnType].apply(packet);
  63. }
  64. return new BinaryRow(columns);
  65. }
  66. }
  67. // TODO: replace with constants.MYSQL_TYPE_*
  68. binaryReader[Types.DECIMAL] = Packet.prototype.readLengthCodedString;
  69. binaryReader[1] = Packet.prototype.readInt8; // tiny
  70. binaryReader[2] = Packet.prototype.readInt16; // short
  71. binaryReader[3] = Packet.prototype.readInt32; // long
  72. binaryReader[4] = Packet.prototype.readFloat; // float
  73. binaryReader[5] = Packet.prototype.readDouble; // double
  74. binaryReader[6] = Packet.prototype.assertInvalid; // null, should be skipped vie null bitmap
  75. binaryReader[7] = Packet.prototype.readTimestamp; // timestamp, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIMESTAMP
  76. binaryReader[8] = Packet.prototype.readInt64; // long long
  77. binaryReader[9] = Packet.prototype.readInt32; // int24
  78. binaryReader[10] = Packet.prototype.readTimestamp; // date
  79. binaryReader[11] = Packet.prototype.readTime; // time, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIME
  80. binaryReader[12] = Packet.prototype.readDateTime; // datetime, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_DATETIME
  81. binaryReader[13] = Packet.prototype.readInt16; // year
  82. binaryReader[Types.VAR_STRING] = Packet.prototype.readLengthCodedString; // var string
  83. module.exports = BinaryRow;