connection-information.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const Queue = require('denque');
  3. const _addPacket = function (msg) {
  4. this.lastPackets.push(msg);
  5. while (this.lastPackets.size() > 32) this.lastPackets.shift();
  6. };
  7. const _getLastPackets = function () {
  8. let output = '';
  9. let packet;
  10. while ((packet = this.lastPackets.shift())) {
  11. output += '\n' + packet;
  12. }
  13. return output;
  14. };
  15. class ConnectionInformation {
  16. constructor() {
  17. this.threadId = -1;
  18. this.status = null;
  19. this.serverVersion = null;
  20. this.serverCapabilities = null;
  21. }
  22. addPacket(msg) {}
  23. getLastPackets() {
  24. return '';
  25. }
  26. enableLogPacket() {
  27. this.lastPackets = new Queue();
  28. this.addPacket = _addPacket.bind(this);
  29. this.getLastPackets = _getLastPackets.bind(this);
  30. }
  31. hasMinVersion(major, minor, patch) {
  32. if (!this.serverVersion)
  33. throw new Error('cannot know if server version until connection is established');
  34. if (!major) throw new Error('a major version must be set');
  35. if (!minor) minor = 0;
  36. if (!patch) patch = 0;
  37. let ver = this.serverVersion;
  38. return (
  39. ver.major > major ||
  40. (ver.major === major && ver.minor > minor) ||
  41. (ver.major === major && ver.minor === minor && ver.patch >= patch)
  42. );
  43. }
  44. isMariaDB() {
  45. if (!this.serverVersion)
  46. throw new Error('cannot know if server is MariaDB until connection is established');
  47. return this.serverVersion.mariaDb;
  48. }
  49. /**
  50. * Parse raw info to set server major/minor/patch values
  51. * @param info
  52. */
  53. static parseVersionString(info) {
  54. let car;
  55. let offset = 0;
  56. let type = 0;
  57. let val = 0;
  58. for (; offset < info.serverVersion.raw.length; offset++) {
  59. car = info.serverVersion.raw.charCodeAt(offset);
  60. if (car < 48 || car > 57) {
  61. switch (type) {
  62. case 0:
  63. info.serverVersion.major = val;
  64. break;
  65. case 1:
  66. info.serverVersion.minor = val;
  67. break;
  68. case 2:
  69. info.serverVersion.patch = val;
  70. return;
  71. }
  72. type++;
  73. val = 0;
  74. } else {
  75. val = val * 10 + car - 48;
  76. }
  77. }
  78. //serverVersion finished by number like "5.5.57", assign patchVersion
  79. if (type === 2) info.serverVersion.patch = val;
  80. }
  81. }
  82. module.exports = ConnectionInformation;