text_row.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const Packet = require('../packets/packet');
  3. class TextRow {
  4. constructor(columns) {
  5. this.columns = columns || [];
  6. }
  7. static fromPacket(packet) {
  8. // packet.reset(); // set offset to starting point?
  9. const columns = [];
  10. while (packet.haveMoreData()) {
  11. columns.push(packet.readLengthCodedString());
  12. }
  13. return new TextRow(columns);
  14. }
  15. static toPacket(columns, encoding) {
  16. const sequenceId = 0; // TODO remove, this is calculated now in connecton
  17. let length = 0;
  18. columns.forEach(val => {
  19. if (val === null || typeof val === 'undefined') {
  20. ++length;
  21. return;
  22. }
  23. length += Packet.lengthCodedStringLength(val.toString(10), encoding);
  24. });
  25. const buffer = Buffer.allocUnsafe(length + 4);
  26. const packet = new Packet(sequenceId, buffer, 0, length + 4);
  27. packet.offset = 4;
  28. columns.forEach(val => {
  29. if (val === null) {
  30. packet.writeNull();
  31. return;
  32. }
  33. if (typeof val === 'undefined') {
  34. packet.writeInt8(0);
  35. return;
  36. }
  37. packet.writeLengthCodedString(val.toString(10), encoding);
  38. });
  39. return packet;
  40. }
  41. }
  42. module.exports = TextRow;