column_definition.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.slice(_nameStart, _nameStart + _nameLength),
  46. this.encoding === 'binary' ? this._clientEncoding : this.encoding
  47. );
  48. this.columnLength = packet.readInt32();
  49. this.columnType = packet.readInt8();
  50. this.flags = packet.readInt16();
  51. this.decimals = packet.readInt8();
  52. }
  53. inspect() {
  54. return {
  55. catalog: this.catalog,
  56. schema: this.schema,
  57. name: this.name,
  58. orgName: this.orgName,
  59. table: this.table,
  60. orgTable: this.orgTable,
  61. characterSet: this.characterSet,
  62. columnLength: this.columnLength,
  63. columnType: this.columnType,
  64. flags: this.flags,
  65. decimals: this.decimals
  66. };
  67. }
  68. static toPacket(column, sequenceId) {
  69. let length = 17; // = 4 padding + 1 + 12 for the rest
  70. fields.forEach(field => {
  71. length += Packet.lengthCodedStringLength(
  72. column[field],
  73. CharsetToEncoding[column.characterSet]
  74. );
  75. });
  76. const buffer = Buffer.allocUnsafe(length);
  77. const packet = new Packet(sequenceId, buffer, 0, length);
  78. function writeField(name) {
  79. packet.writeLengthCodedString(
  80. column[name],
  81. CharsetToEncoding[column.characterSet]
  82. );
  83. }
  84. packet.offset = 4;
  85. fields.forEach(writeField);
  86. packet.writeInt8(0x0c);
  87. packet.writeInt16(column.characterSet);
  88. packet.writeInt32(column.columnLength);
  89. packet.writeInt8(column.columnType);
  90. packet.writeInt16(column.flags);
  91. packet.writeInt8(column.decimals);
  92. packet.writeInt16(0); // filler
  93. return packet;
  94. }
  95. // node-mysql compatibility: alias "db" to "schema"
  96. get db() {
  97. const start = this._schemaStart;
  98. const end = start._shemaLength;
  99. return this._buf.utf8Slice(start, end);
  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. return StringParser.decode(
  108. this._buf.slice(start, end),
  109. this.encoding === 'binary' ? this._clientEncoding : this.encoding
  110. );
  111. }
  112. });
  113. };
  114. addString('catalog');
  115. addString('schema');
  116. addString('table');
  117. addString('orgTable');
  118. addString('orgName');
  119. module.exports = ColumnDefinition;