column_definition.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const Packet = require('../packets/packet');
  3. const StringParser = require('../parsers/string');
  4. const CharsetToEncoding = require('../constants/charset_encodings.js');
  5. const fields = ['catalog', 'schema', 'table', 'orgTable', 'name', 'orgName'];
  6. // creating JS string is relatively expensive (compared to
  7. // reading few bytes from buffer) because all string properties
  8. // except for name are unlikely to be used we postpone
  9. // string conversion until property access
  10. //
  11. // TODO: watch for integration benchmarks (one with real network buffer)
  12. // there could be bad side effect as keeping reference to a buffer makes it
  13. // sit in the memory longer (usually until final .query() callback)
  14. // Latest v8 perform much better in regard to bufferer -> string conversion,
  15. // at some point of time this optimisation might become unnecessary
  16. // see https://github.com/sidorares/node-mysql2/pull/137
  17. //
  18. class ColumnDefinition {
  19. constructor(packet, clientEncoding) {
  20. this._buf = packet.buffer;
  21. this._clientEncoding = clientEncoding;
  22. this._catalogLength = packet.readLengthCodedNumber();
  23. this._catalogStart = packet.offset;
  24. packet.offset += this._catalogLength;
  25. this._schemaLength = packet.readLengthCodedNumber();
  26. this._schemaStart = packet.offset;
  27. packet.offset += this._schemaLength;
  28. this._tableLength = packet.readLengthCodedNumber();
  29. this._tableStart = packet.offset;
  30. packet.offset += this._tableLength;
  31. this._orgTableLength = packet.readLengthCodedNumber();
  32. this._orgTableStart = packet.offset;
  33. packet.offset += this._orgTableLength;
  34. // name is always used, don't make it lazy
  35. const _nameLength = packet.readLengthCodedNumber();
  36. const _nameStart = packet.offset;
  37. packet.offset += _nameLength;
  38. this._orgNameLength = packet.readLengthCodedNumber();
  39. this._orgNameStart = packet.offset;
  40. packet.offset += this._orgNameLength;
  41. packet.skip(1); // length of the following fields (always 0x0c)
  42. this.characterSet = packet.readInt16();
  43. this.encoding = CharsetToEncoding[this.characterSet];
  44. this.name = StringParser.decode(
  45. this._buf,
  46. this.encoding === 'binary' ? this._clientEncoding : this.encoding,
  47. _nameStart,
  48. _nameStart + _nameLength
  49. );
  50. this.columnLength = packet.readInt32();
  51. this.columnType = packet.readInt8();
  52. this.flags = packet.readInt16();
  53. this.decimals = packet.readInt8();
  54. }
  55. inspect() {
  56. return {
  57. catalog: this.catalog,
  58. schema: this.schema,
  59. name: this.name,
  60. orgName: this.orgName,
  61. table: this.table,
  62. orgTable: this.orgTable,
  63. characterSet: this.characterSet,
  64. columnLength: this.columnLength,
  65. columnType: this.columnType,
  66. flags: this.flags,
  67. decimals: this.decimals
  68. };
  69. }
  70. static toPacket(column, sequenceId) {
  71. let length = 17; // = 4 padding + 1 + 12 for the rest
  72. fields.forEach(field => {
  73. length += Packet.lengthCodedStringLength(
  74. column[field],
  75. CharsetToEncoding[column.characterSet]
  76. );
  77. });
  78. const buffer = Buffer.allocUnsafe(length);
  79. const packet = new Packet(sequenceId, buffer, 0, length);
  80. function writeField(name) {
  81. packet.writeLengthCodedString(
  82. column[name],
  83. CharsetToEncoding[column.characterSet]
  84. );
  85. }
  86. packet.offset = 4;
  87. fields.forEach(writeField);
  88. packet.writeInt8(0x0c);
  89. packet.writeInt16(column.characterSet);
  90. packet.writeInt32(column.columnLength);
  91. packet.writeInt8(column.columnType);
  92. packet.writeInt16(column.flags);
  93. packet.writeInt8(column.decimals);
  94. packet.writeInt16(0); // filler
  95. return packet;
  96. }
  97. // node-mysql compatibility: alias "db" to "schema"
  98. get db() {
  99. return this.schema;
  100. }
  101. }
  102. const addString = function(name) {
  103. Object.defineProperty(ColumnDefinition.prototype, name, {
  104. get: function() {
  105. const start = this[`_${name}Start`];
  106. const end = start + this[`_${name}Length`];
  107. const val = StringParser.decode(
  108. this._buf,
  109. this.encoding === 'binary' ? this._clientEncoding : this.encoding,
  110. start,
  111. end
  112. );
  113. Object.defineProperty(this, name, {
  114. value: val,
  115. writable: false,
  116. configurable: false,
  117. enumerable: false
  118. });
  119. return val;
  120. }
  121. });
  122. };
  123. addString('catalog');
  124. addString('schema');
  125. addString('table');
  126. addString('orgTable');
  127. addString('orgName');
  128. module.exports = ColumnDefinition;