stream.js 894 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const Query = require('./query');
  3. const { Readable } = require('stream');
  4. /**
  5. * Protocol COM_QUERY with streaming events.
  6. * see : https://mariadb.com/kb/en/library/com_query/
  7. */
  8. class Stream extends Query {
  9. constructor(cmdOpts, connOpts, sql, values, socket) {
  10. super(
  11. () => {},
  12. () => {},
  13. cmdOpts,
  14. connOpts,
  15. sql,
  16. values
  17. );
  18. this.socket = socket;
  19. this.inStream = new Readable({
  20. objectMode: true,
  21. read: () => {}
  22. });
  23. this.on('fields', function (meta) {
  24. this.inStream.emit('fields', meta);
  25. });
  26. this.on('error', function (err) {
  27. this.inStream.emit('error', err);
  28. });
  29. this.on('end', function (err) {
  30. if (err) this.inStream.emit('error', err);
  31. this.inStream.push(null);
  32. });
  33. }
  34. handleNewRows(row) {
  35. this.inStream.push(row);
  36. }
  37. }
  38. module.exports = Stream;