column-definition.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const Collations = require('../const/collations.js');
  3. const FieldType = require('../const/field-type');
  4. const Capabilities = require('../const/capabilities');
  5. /**
  6. * Column definition
  7. * see https://mariadb.com/kb/en/library/resultset/#column-definition-packet
  8. */
  9. class ColumnDef {
  10. constructor(packet, info) {
  11. this._parse = new StringParser(packet);
  12. if (info.serverCapabilities & Capabilities.MARIADB_CLIENT_EXTENDED_TYPE_INFO) {
  13. const subPacket = packet.subPacketLengthEncoded();
  14. while (subPacket.remaining()) {
  15. switch (subPacket.readUInt8()) {
  16. case 0:
  17. this.dataTypeName = subPacket.readAsciiStringLengthEncoded();
  18. break;
  19. case 1:
  20. this.dataTypeFormat = subPacket.readAsciiStringLengthEncoded();
  21. break;
  22. default:
  23. // skip data
  24. const len = subPacket.readUnsignedLength();
  25. if (len) {
  26. subPacket.skip(len);
  27. }
  28. break;
  29. }
  30. }
  31. }
  32. packet.skip(1); // length of fixed fields
  33. this.collation = Collations.fromIndex(packet.readUInt16());
  34. this.columnLength = packet.readUInt32();
  35. this.columnType = packet.readUInt8();
  36. this.flags = packet.readUInt16();
  37. this.scale = packet.readUInt8();
  38. this.type = FieldType.TYPES[this.columnType];
  39. }
  40. db() {
  41. return this._parse.packet.readString(this._parse.dbOffset, this._parse.dbLength);
  42. }
  43. schema() {
  44. return this._parse.packet.readString(this._parse.dbOffset, this._parse.dbLength);
  45. }
  46. table() {
  47. return this._parse.packet.readString(this._parse.tableOffset, this._parse.tableLength);
  48. }
  49. orgTable() {
  50. return this._parse.packet.readString(this._parse.orgTableOffset, this._parse.orgTableLength);
  51. }
  52. name() {
  53. return this._parse.packet.readString(this._parse.nameOffset, this._parse.nameLength);
  54. }
  55. orgName() {
  56. return this._parse.packet.readString(this._parse.orgNameOffset, this._parse.orgNameLength);
  57. }
  58. }
  59. /**
  60. * String parser.
  61. * This object permits to avoid listing all private information to metadata object.
  62. */
  63. class StringParser {
  64. constructor(packet) {
  65. packet.skip(4); // skip 'def'
  66. this.dbLength = packet.readUnsignedLength();
  67. this.dbOffset = packet.pos;
  68. packet.skip(this.dbLength);
  69. this.tableLength = packet.readUnsignedLength();
  70. this.tableOffset = packet.pos;
  71. packet.skip(this.tableLength);
  72. this.orgTableLength = packet.readUnsignedLength();
  73. this.orgTableOffset = packet.pos;
  74. packet.skip(this.orgTableLength);
  75. this.nameLength = packet.readUnsignedLength();
  76. this.nameOffset = packet.pos;
  77. packet.skip(this.nameLength);
  78. this.orgNameLength = packet.readUnsignedLength();
  79. this.orgNameOffset = packet.pos;
  80. packet.skip(this.orgNameLength);
  81. this.packet = packet;
  82. }
  83. }
  84. module.exports = ColumnDef;