binary_row.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. toPacket() {
  10. throw new Error('Not implemented');
  11. }
  12. // TODO: complete list of types...
  13. static fromPacket(fields, packet) {
  14. const columns = new Array(fields.length);
  15. packet.readInt8(); // TODO check it's 0
  16. const nullBitmapLength = Math.floor((fields.length + 7 + 2) / 8);
  17. // TODO: read and interpret null bitmap
  18. packet.skip(nullBitmapLength);
  19. for (let i = 0; i < columns.length; ++i) {
  20. columns[i] = binaryReader[fields[i].columnType].apply(packet);
  21. }
  22. return new BinaryRow(columns);
  23. }
  24. }
  25. // TODO: replace with constants.MYSQL_TYPE_*
  26. binaryReader[Types.DECIMAL] = Packet.prototype.readLengthCodedString;
  27. binaryReader[1] = Packet.prototype.readInt8; // tiny
  28. binaryReader[2] = Packet.prototype.readInt16; // short
  29. binaryReader[3] = Packet.prototype.readInt32; // long
  30. binaryReader[4] = Packet.prototype.readFloat; // float
  31. binaryReader[5] = Packet.prototype.readDouble; // double
  32. binaryReader[6] = Packet.prototype.assertInvalid; // null, should be skipped vie null bitmap
  33. binaryReader[7] = Packet.prototype.readTimestamp; // timestamp, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIMESTAMP
  34. binaryReader[8] = Packet.prototype.readInt64; // long long
  35. binaryReader[9] = Packet.prototype.readInt32; // int24
  36. binaryReader[10] = Packet.prototype.readTimestamp; // date
  37. binaryReader[11] = Packet.prototype.readTime; // time, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_TIME
  38. binaryReader[12] = Packet.prototype.readDateTime; // datetime, http://dev.mysql.com/doc/internals/en/prepared-statements.html#packet-ProtocolBinary::MYSQL_TYPE_DATETIME
  39. binaryReader[13] = Packet.prototype.readInt16; // year
  40. binaryReader[Types.VAR_STRING] = Packet.prototype.readLengthCodedString; // var string
  41. module.exports = BinaryRow;