test-connection-config-flags-affected-rows.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Based on:
  2. // https://github.com/ichernev/node-mysql/blob/on-duplicate-key-update/test/integration/connection/test-on-duplicate-key-update.js
  3. // (but with CLIENT_FOUND_ROWS connection flag blacklisted)
  4. var common = require('../../common');
  5. var connection = common.createConnection({ flags: "-FOUND_ROWS" });
  6. var assert = require('assert');
  7. common.useTestDb(connection);
  8. var table = 'on_duplicate_key_test';
  9. connection.query('DROP TABLE IF EXISTS `' + table + '`');
  10. connection.query([
  11. 'CREATE TABLE `' + table + '` (',
  12. '`a` int(11) unsigned NOT NULL AUTO_INCREMENT,',
  13. '`b` int(11),',
  14. '`c` int(11),',
  15. 'PRIMARY KEY (`a`)',
  16. ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  17. ].join('\n'));
  18. connection.query('INSERT INTO `' + table + '` SET ?', {a: 1, b: 1, c: 1});
  19. connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE c = 1', function(err, info) {
  20. assert.strictEqual(null, err);
  21. assert.strictEqual(0, info.affectedRows, 'both primary key and updated key are the same so nothing is affected (expected 0, got ' + info.affectedRows + ' affectedRows)');
  22. });
  23. connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (2, 3, 4) ON DUPLICATE KEY UPDATE c = 1', function(err, info) {
  24. assert.strictEqual(null, err);
  25. assert.strictEqual(1, info.affectedRows, 'primary key differs, so new row is inserted (expected 1, got ' + info.affectedRows + ' affectedRows)');
  26. });
  27. connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE c = 2', function(err, info) {
  28. assert.strictEqual(null, err);
  29. assert.strictEqual(2, info.affectedRows, 'primary key is the same, row is updated (expected 2, got ' + info.affectedRows + ' affectedRows)');
  30. });
  31. connection.end();