index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _map2 = require('lodash/map');
  4. var _map3 = _interopRequireDefault(_map2);
  5. var _assign2 = require('lodash/assign');
  6. var _assign3 = _interopRequireDefault(_assign2);
  7. var _inherits = require('inherits');
  8. var _inherits2 = _interopRequireDefault(_inherits);
  9. var _client = require('../../client');
  10. var _client2 = _interopRequireDefault(_client);
  11. var _bluebird = require('bluebird');
  12. var _bluebird2 = _interopRequireDefault(_bluebird);
  13. var _helpers = require('../../helpers');
  14. var helpers = _interopRequireWildcard(_helpers);
  15. var _transaction = require('./transaction');
  16. var _transaction2 = _interopRequireDefault(_transaction);
  17. var _compiler = require('./query/compiler');
  18. var _compiler2 = _interopRequireDefault(_compiler);
  19. var _compiler3 = require('./schema/compiler');
  20. var _compiler4 = _interopRequireDefault(_compiler3);
  21. var _tablecompiler = require('./schema/tablecompiler');
  22. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  23. var _columncompiler = require('./schema/columncompiler');
  24. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  25. var _string = require('../../query/string');
  26. 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; } }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. // Always initialize with the "QueryBuilder" and "QueryCompiler"
  29. // objects, which extend the base 'lib/query/builder' and
  30. // 'lib/query/compiler', respectively.
  31. // MySQL Client
  32. // -------
  33. function Client_MySQL(config) {
  34. _client2.default.call(this, config);
  35. }
  36. (0, _inherits2.default)(Client_MySQL, _client2.default);
  37. (0, _assign3.default)(Client_MySQL.prototype, {
  38. dialect: 'mysql',
  39. driverName: 'mysql',
  40. _driver: function _driver() {
  41. return require('mysql');
  42. },
  43. queryCompiler: function queryCompiler() {
  44. return new (Function.prototype.bind.apply(_compiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  45. },
  46. schemaCompiler: function schemaCompiler() {
  47. return new (Function.prototype.bind.apply(_compiler4.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  48. },
  49. tableCompiler: function tableCompiler() {
  50. return new (Function.prototype.bind.apply(_tablecompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  51. },
  52. columnCompiler: function columnCompiler() {
  53. return new (Function.prototype.bind.apply(_columncompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  54. },
  55. transaction: function transaction() {
  56. return new (Function.prototype.bind.apply(_transaction2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  57. },
  58. _escapeBinding: (0, _string.makeEscape)(),
  59. wrapIdentifier: function wrapIdentifier(value) {
  60. return value !== '*' ? '`' + value.replace(/`/g, '``') + '`' : '*';
  61. },
  62. // Get a raw connection, called by the `pool` whenever a new
  63. // connection needs to be added to the pool.
  64. acquireRawConnection: function acquireRawConnection() {
  65. var _this = this;
  66. return new _bluebird2.default(function (resolver, rejecter) {
  67. var connection = _this.driver.createConnection(_this.connectionSettings);
  68. connection.connect(function (err) {
  69. if (err) return rejecter(err);
  70. connection.on('error', function (err) {
  71. connection.__knex__disposed = err;
  72. });
  73. resolver(connection);
  74. });
  75. });
  76. },
  77. // Used to explicitly close a connection, called internally by the pool
  78. // when a connection times out or the pool is shutdown.
  79. destroyRawConnection: function destroyRawConnection(connection) {
  80. connection.removeAllListeners();
  81. connection.end(function (err) {
  82. if (err) connection.__knex__disposed = err;
  83. });
  84. },
  85. validateConnection: function validateConnection(connection) {
  86. return connection.state === 'connected' || connection.state === 'authenticated';
  87. },
  88. // Grab a connection, run the query via the MySQL streaming interface,
  89. // and pass that through to the stream we've sent back to the client.
  90. _stream: function _stream(connection, obj, stream, options) {
  91. options = options || {};
  92. return new _bluebird2.default(function (resolver, rejecter) {
  93. stream.on('error', rejecter);
  94. stream.on('end', resolver);
  95. connection.query(obj.sql, obj.bindings).stream(options).pipe(stream);
  96. });
  97. },
  98. // Runs the query on the specified connection, providing the bindings
  99. // and any other necessary prep work.
  100. _query: function _query(connection, obj) {
  101. if (!obj || typeof obj === 'string') obj = { sql: obj };
  102. return new _bluebird2.default(function (resolver, rejecter) {
  103. var _obj = obj,
  104. sql = _obj.sql;
  105. if (!sql) return resolver();
  106. if (obj.options) sql = (0, _assign3.default)({ sql: sql }, obj.options);
  107. connection.query(sql, obj.bindings, function (err, rows, fields) {
  108. if (err) return rejecter(err);
  109. obj.response = [rows, fields];
  110. resolver(obj);
  111. });
  112. });
  113. },
  114. // Process the response as returned from the query.
  115. processResponse: function processResponse(obj, runner) {
  116. if (obj == null) return;
  117. var response = obj.response;
  118. var method = obj.method;
  119. var rows = response[0];
  120. var fields = response[1];
  121. if (obj.output) return obj.output.call(runner, rows, fields);
  122. switch (method) {
  123. case 'select':
  124. case 'pluck':
  125. case 'first':
  126. {
  127. var resp = helpers.skim(rows);
  128. if (method === 'pluck') return (0, _map3.default)(resp, obj.pluck);
  129. return method === 'first' ? resp[0] : resp;
  130. }
  131. case 'insert':
  132. return [rows.insertId];
  133. case 'del':
  134. case 'update':
  135. case 'counter':
  136. return rows.affectedRows;
  137. default:
  138. return response;
  139. }
  140. },
  141. canCancelQuery: true,
  142. cancelQuery: function cancelQuery(connectionToKill) {
  143. var _this2 = this;
  144. var acquiringConn = this.acquireConnection();
  145. // Error out if we can't acquire connection in time.
  146. // Purposely not putting timeout on `KILL QUERY` execution because erroring
  147. // early there would release the `connectionToKill` back to the pool with
  148. // a `KILL QUERY` command yet to finish.
  149. return acquiringConn.timeout(100).then(function (conn) {
  150. return _this2.query(conn, {
  151. method: 'raw',
  152. sql: 'KILL QUERY ?',
  153. bindings: [connectionToKill.threadId],
  154. options: {}
  155. });
  156. }).finally(function () {
  157. // NOT returning this promise because we want to release the connection
  158. // in a non-blocking fashion
  159. acquiringConn.then(function (conn) {
  160. return _this2.releaseConnection(conn);
  161. });
  162. });
  163. }
  164. });
  165. exports.default = Client_MySQL;
  166. module.exports = exports['default'];