FieldPacket.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module.exports = FieldPacket;
  2. function FieldPacket(options) {
  3. options = options || {};
  4. this.catalog = options.catalog;
  5. this.db = options.db;
  6. this.table = options.table;
  7. this.orgTable = options.orgTable;
  8. this.name = options.name;
  9. this.orgName = options.orgName;
  10. this.charsetNr = options.charsetNr;
  11. this.length = options.length;
  12. this.type = options.type;
  13. this.flags = options.flags;
  14. this.decimals = options.decimals;
  15. this.default = options.default;
  16. this.zeroFill = options.zeroFill;
  17. this.protocol41 = options.protocol41;
  18. }
  19. FieldPacket.prototype.parse = function(parser) {
  20. if (this.protocol41) {
  21. this.catalog = parser.parseLengthCodedString();
  22. this.db = parser.parseLengthCodedString();
  23. this.table = parser.parseLengthCodedString();
  24. this.orgTable = parser.parseLengthCodedString();
  25. this.name = parser.parseLengthCodedString();
  26. this.orgName = parser.parseLengthCodedString();
  27. if (parser.parseLengthCodedNumber() !== 0x0c) {
  28. var err = new TypeError('Received invalid field length');
  29. err.code = 'PARSER_INVALID_FIELD_LENGTH';
  30. throw err;
  31. }
  32. this.charsetNr = parser.parseUnsignedNumber(2);
  33. this.length = parser.parseUnsignedNumber(4);
  34. this.type = parser.parseUnsignedNumber(1);
  35. this.flags = parser.parseUnsignedNumber(2);
  36. this.decimals = parser.parseUnsignedNumber(1);
  37. var filler = parser.parseBuffer(2);
  38. if (filler[0] !== 0x0 || filler[1] !== 0x0) {
  39. var err = new TypeError('Received invalid filler');
  40. err.code = 'PARSER_INVALID_FILLER';
  41. throw err;
  42. }
  43. // parsed flags
  44. this.zeroFill = (this.flags & 0x0040 ? true : false);
  45. if (parser.reachedPacketEnd()) {
  46. return;
  47. }
  48. this.default = parser.parseLengthCodedString();
  49. } else {
  50. this.table = parser.parseLengthCodedString();
  51. this.name = parser.parseLengthCodedString();
  52. this.length = parser.parseUnsignedNumber(parser.parseUnsignedNumber(1));
  53. this.type = parser.parseUnsignedNumber(parser.parseUnsignedNumber(1));
  54. }
  55. };
  56. FieldPacket.prototype.write = function(writer) {
  57. if (this.protocol41) {
  58. writer.writeLengthCodedString(this.catalog);
  59. writer.writeLengthCodedString(this.db);
  60. writer.writeLengthCodedString(this.table);
  61. writer.writeLengthCodedString(this.orgTable);
  62. writer.writeLengthCodedString(this.name);
  63. writer.writeLengthCodedString(this.orgName);
  64. writer.writeLengthCodedNumber(0x0c);
  65. writer.writeUnsignedNumber(2, this.charsetNr || 0);
  66. writer.writeUnsignedNumber(4, this.length || 0);
  67. writer.writeUnsignedNumber(1, this.type || 0);
  68. writer.writeUnsignedNumber(2, this.flags || 0);
  69. writer.writeUnsignedNumber(1, this.decimals || 0);
  70. writer.writeFiller(2);
  71. if (this.default !== undefined) {
  72. writer.writeLengthCodedString(this.default);
  73. }
  74. } else {
  75. writer.writeLengthCodedString(this.table);
  76. writer.writeLengthCodedString(this.name);
  77. writer.writeUnsignedNumber(1, 0x01);
  78. writer.writeUnsignedNumber(1, this.length);
  79. writer.writeUnsignedNumber(1, 0x01);
  80. writer.writeUnsignedNumber(1, this.type);
  81. }
  82. };