index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const AuthSwitchRequest = require('./auth_switch_request');
  3. const AuthSwitchRequestMoreData = require('./auth_switch_request_more_data');
  4. const AuthSwitchResponse = require('./auth_switch_response');
  5. const BinaryRow = require('./binary_row');
  6. const BinlogDump = require('./binlog_dump');
  7. const ChangeUser = require('./change_user');
  8. const CloseStatement = require('./close_statement');
  9. const ColumnDefinition = require('./column_definition');
  10. const Execute = require('./execute');
  11. const Handshake = require('./handshake');
  12. const HandshakeResponse = require('./handshake_response');
  13. const PrepareStatement = require('./prepare_statement');
  14. const PreparedStatementHeader = require('./prepared_statement_header');
  15. const Query = require('./query');
  16. const RegisterSlave = require('./register_slave');
  17. const ResultsetHeader = require('./resultset_header');
  18. const SslRequest = require('./ssl_request');
  19. const TextRow = require('./text_row');
  20. const ctorArr = [
  21. AuthSwitchRequest,
  22. AuthSwitchRequestMoreData,
  23. AuthSwitchResponse,
  24. BinaryRow,
  25. BinlogDump,
  26. ChangeUser,
  27. CloseStatement,
  28. ColumnDefinition,
  29. Execute,
  30. Handshake,
  31. HandshakeResponse,
  32. PrepareStatement,
  33. PreparedStatementHeader,
  34. Query,
  35. RegisterSlave,
  36. ResultsetHeader,
  37. SslRequest,
  38. TextRow
  39. ];
  40. ctorArr.forEach(ctor => {
  41. module.exports[ctor.name] = ctor;
  42. // monkey-patch it to include name if debug is on
  43. if (process.env.NODE_DEBUG) {
  44. if (ctor.prototype.toPacket) {
  45. const old = ctor.prototype.toPacket;
  46. ctor.prototype.toPacket = function() {
  47. const p = old.call(this);
  48. p._name = ctor.name;
  49. return p;
  50. };
  51. }
  52. }
  53. });
  54. // simple packets:
  55. const Packet = require('./packet');
  56. exports.Packet = Packet;
  57. class OK {
  58. static toPacket(args, encoding) {
  59. args = args || {};
  60. const affectedRows = args.affectedRows || 0;
  61. const insertId = args.insertId || 0;
  62. const serverStatus = args.serverStatus || 0;
  63. const warningCount = args.warningCount || 0;
  64. const message = args.message || '';
  65. let length = 9 + Packet.lengthCodedNumberLength(affectedRows);
  66. length += Packet.lengthCodedNumberLength(insertId);
  67. const buffer = Buffer.allocUnsafe(length);
  68. const packet = new Packet(0, buffer, 0, length);
  69. packet.offset = 4;
  70. packet.writeInt8(0);
  71. packet.writeLengthCodedNumber(affectedRows);
  72. packet.writeLengthCodedNumber(insertId);
  73. packet.writeInt16(serverStatus);
  74. packet.writeInt16(warningCount);
  75. packet.writeString(message, encoding);
  76. packet._name = 'OK';
  77. return packet;
  78. }
  79. }
  80. exports.OK = OK;
  81. // warnings, statusFlags
  82. class EOF {
  83. static toPacket(warnings, statusFlags) {
  84. if (typeof warnings === 'undefined') {
  85. warnings = 0;
  86. }
  87. if (typeof statusFlags === 'undefined') {
  88. statusFlags = 0;
  89. }
  90. const packet = new Packet(0, Buffer.allocUnsafe(9), 0, 9);
  91. packet.offset = 4;
  92. packet.writeInt8(0xfe);
  93. packet.writeInt16(warnings);
  94. packet.writeInt16(statusFlags);
  95. packet._name = 'EOF';
  96. return packet;
  97. }
  98. }
  99. exports.EOF = EOF;
  100. class Error {
  101. static toPacket(args, encoding) {
  102. const length = 13 + Buffer.byteLength(args.message, 'utf8');
  103. const packet = new Packet(0, Buffer.allocUnsafe(length), 0, length);
  104. packet.offset = 4;
  105. packet.writeInt8(0xff);
  106. packet.writeInt16(args.code);
  107. // TODO: sql state parameter
  108. packet.writeString('#_____', encoding);
  109. packet.writeString(args.message, encoding);
  110. packet._name = 'Error';
  111. return packet;
  112. }
  113. }
  114. exports.Error = Error;