OkPacket.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. module.exports = OkPacket;
  2. function OkPacket(options) {
  3. options = options || {};
  4. this.fieldCount = undefined;
  5. this.affectedRows = undefined;
  6. this.insertId = undefined;
  7. this.serverStatus = undefined;
  8. this.warningCount = undefined;
  9. this.message = undefined;
  10. this.protocol41 = options.protocol41;
  11. }
  12. OkPacket.prototype.parse = function(parser) {
  13. this.fieldCount = parser.parseUnsignedNumber(1);
  14. this.affectedRows = parser.parseLengthCodedNumber();
  15. this.insertId = parser.parseLengthCodedNumber();
  16. if (this.protocol41) {
  17. this.serverStatus = parser.parseUnsignedNumber(2);
  18. this.warningCount = parser.parseUnsignedNumber(2);
  19. }
  20. this.message = parser.parsePacketTerminatedString();
  21. this.changedRows = 0;
  22. var m = this.message.match(/\schanged:\s*(\d+)/i);
  23. if (m !== null) {
  24. this.changedRows = parseInt(m[1], 10);
  25. }
  26. };
  27. OkPacket.prototype.write = function(writer) {
  28. writer.writeUnsignedNumber(1, 0x00);
  29. writer.writeLengthCodedNumber(this.affectedRows || 0);
  30. writer.writeLengthCodedNumber(this.insertId || 0);
  31. if (this.protocol41) {
  32. writer.writeUnsignedNumber(2, this.serverStatus || 0);
  33. writer.writeUnsignedNumber(2, this.warningCount || 0);
  34. }
  35. writer.writeString(this.message);
  36. };