query.js 775 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. const Packet = require('../packets/packet.js');
  3. const CommandCode = require('../constants/commands.js');
  4. const StringParser = require('../parsers/string.js');
  5. const CharsetToEncoding = require('../constants/charset_encodings.js');
  6. class Query {
  7. constructor(sql, charsetNumber) {
  8. this.query = sql;
  9. this.charsetNumber = charsetNumber;
  10. this.encoding = CharsetToEncoding[charsetNumber];
  11. }
  12. toPacket() {
  13. const buf = StringParser.encode(this.query, this.encoding);
  14. const length = 5 + buf.length;
  15. const buffer = Buffer.allocUnsafe(length);
  16. const packet = new Packet(0, buffer, 0, length);
  17. packet.offset = 4;
  18. packet.writeInt8(CommandCode.QUERY);
  19. packet.writeBuffer(buf);
  20. return packet;
  21. }
  22. }
  23. module.exports = Query;