test-load-data-infile.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var common = require('../../common');
  2. var connection = common.createConnection();
  3. var assert = require('assert');
  4. common.useTestDb(connection);
  5. var table = 'load_data_test';
  6. connection.query([
  7. 'CREATE TEMPORARY TABLE `' + table + '` (',
  8. '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
  9. '`title` varchar(255),',
  10. 'PRIMARY KEY (`id`)',
  11. ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  12. ].join('\n'));
  13. var path = common.fixtures + '/data.csv';
  14. var sql =
  15. 'LOAD DATA LOCAL INFILE ? INTO TABLE ' + table + ' ' +
  16. 'FIELDS TERMINATED BY ? (id, title)';
  17. var ok;
  18. connection.query(sql, [path, ','], function(err, _ok) {
  19. if (err) throw err;
  20. ok = _ok;
  21. });
  22. var rows;
  23. connection.query('SELECT * FROM ' + table, function(err, _rows) {
  24. if (err) throw err;
  25. rows = _rows;
  26. });
  27. // Try to load a file that does not exist to see if we handle this properly
  28. var loadErr;
  29. var loadResult;
  30. var badPath = common.fixtures + '/does_not_exist.csv';
  31. connection.query(sql, [badPath, ','], function(err, result) {
  32. loadErr = err;
  33. loadResult = result;
  34. });
  35. connection.end();
  36. process.on('exit', function() {
  37. assert.equal(ok.affectedRows, 3);
  38. assert.equal(rows.length, 3);
  39. assert.equal(rows[0].id, 1);
  40. assert.equal(rows[0].title, 'Hello World');
  41. assert.equal(loadErr.code, 'ENOENT');
  42. assert.equal(loadResult.affectedRows, 0);
  43. });