OkPacket.js 1.4 KB

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