index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _map2 = require('lodash/map');
  6. var _map3 = _interopRequireDefault(_map2);
  7. var _pick2 = require('lodash/pick');
  8. var _pick3 = _interopRequireDefault(_pick2);
  9. var _inherits = require('inherits');
  10. var _inherits2 = _interopRequireDefault(_inherits);
  11. var _mysql = require('../mysql');
  12. var _mysql2 = _interopRequireDefault(_mysql);
  13. var _bluebird = require('bluebird');
  14. var _bluebird2 = _interopRequireDefault(_bluebird);
  15. var _helpers = require('../../helpers');
  16. var helpers = _interopRequireWildcard(_helpers);
  17. var _transaction = require('./transaction');
  18. var _transaction2 = _interopRequireDefault(_transaction);
  19. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. var configOptions = ['isServer', 'stream', 'host', 'port', 'localAddress', 'socketPath', 'user', 'password', 'passwordSha1', 'database', 'connectTimeout', 'insecureAuth', 'supportBigNumbers', 'bigNumberStrings', 'decimalNumbers', 'dateStrings', 'debug', 'trace', 'stringifyObjects', 'timezone', 'flags', 'queryFormat', 'pool', 'ssl', 'multipleStatements', 'namedPlaceholders', 'typeCast', 'charsetNumber', 'compress'];
  22. // Always initialize with the "QueryBuilder" and "QueryCompiler"
  23. // objects, which extend the base 'lib/query/builder' and
  24. // 'lib/query/compiler', respectively.
  25. // MySQL2 Client
  26. // -------
  27. function Client_MySQL2(config) {
  28. _mysql2.default.call(this, config);
  29. }
  30. (0, _inherits2.default)(Client_MySQL2, _mysql2.default);
  31. (0, _assign3.default)(Client_MySQL2.prototype, {
  32. // The "dialect", for reference elsewhere.
  33. driverName: 'mysql2',
  34. transaction: function transaction() {
  35. return new (Function.prototype.bind.apply(_transaction2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  36. },
  37. _driver: function _driver() {
  38. return require('mysql2');
  39. },
  40. validateConnection: function validateConnection() {
  41. return true;
  42. },
  43. // Get a raw connection, called by the `pool` whenever a new
  44. // connection needs to be added to the pool.
  45. acquireRawConnection: function acquireRawConnection() {
  46. var connection = this.driver.createConnection((0, _pick3.default)(this.connectionSettings, configOptions));
  47. return new _bluebird2.default(function (resolver, rejecter) {
  48. connection.connect(function (err) {
  49. if (err) {
  50. return rejecter(err);
  51. }
  52. connection.on('error', function (err) {
  53. connection.__knex__disposed = err;
  54. });
  55. resolver(connection);
  56. });
  57. });
  58. },
  59. processResponse: function processResponse(obj, runner) {
  60. var response = obj.response;
  61. var method = obj.method;
  62. var rows = response[0];
  63. var fields = response[1];
  64. if (obj.output) return obj.output.call(runner, rows, fields);
  65. switch (method) {
  66. case 'select':
  67. case 'pluck':
  68. case 'first':
  69. {
  70. var resp = helpers.skim(rows);
  71. if (method === 'pluck') return (0, _map3.default)(resp, obj.pluck);
  72. return method === 'first' ? resp[0] : resp;
  73. }
  74. case 'insert':
  75. return [rows.insertId];
  76. case 'del':
  77. case 'update':
  78. case 'counter':
  79. return rows.affectedRows;
  80. default:
  81. return response;
  82. }
  83. }
  84. });
  85. exports.default = Client_MySQL2;
  86. module.exports = exports['default'];