test-multiple-statements-streaming.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var common = require('../../common');
  2. var connection = common.createConnection({multipleStatements: true});
  3. var assert = require('assert');
  4. var sql = [
  5. 'SELECT 1',
  6. 'USE ' + common.testDatabase,
  7. 'SELECT 2',
  8. 'invalid sql',
  9. 'SELECT 3',
  10. ].join('; ');
  11. var results = [];
  12. var fields = [];
  13. var hadErr = false;
  14. var query = connection.query(sql);
  15. query
  16. .on('error', function(err) {
  17. assert.equal(hadErr, false);
  18. hadErr = true;
  19. assert.equal(err.code, 'ER_PARSE_ERROR');
  20. assert.equal(err.index, 3);
  21. })
  22. .on('fields', function(_fields, index) {
  23. fields.push({fields: _fields, index: index});
  24. })
  25. .on('result', function(result, index) {
  26. results.push({result: result, index: index});
  27. });
  28. connection.end();
  29. process.on('exit', function() {
  30. assert.ok(hadErr);
  31. assert.equal(results.length, 3);
  32. assert.deepEqual(results[0].result, {1: 1});
  33. assert.equal(results[0].index, 0);
  34. assert.equal(results[1].result.constructor.name, 'OkPacket');
  35. assert.equal(results[1].index, 1);
  36. assert.deepEqual(results[2].result, {2: 2});
  37. assert.equal(results[2].index, 2);
  38. assert.equal(fields.length, 2);
  39. assert.equal(fields[0].fields[0].name, '1');
  40. assert.equal(fields[1].fields[0].name, '2');
  41. assert.equal(fields[0].index, 0);
  42. assert.equal(fields[1].index, 2);
  43. });