index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. wrapIdentifierImpl: function wrapIdentifierImpl(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.on('error', function (err) {
  69. connection.__knex__disposed = err;
  70. });
  71. connection.connect(function (err) {
  72. if (err) {
  73. // if connection is rejected, remove listener that was registered above...
  74. connection.removeAllListeners();
  75. return rejecter(err);
  76. }
  77. resolver(connection);
  78. });
  79. });
  80. },
  81. // Used to explicitly close a connection, called internally by the pool
  82. // when a connection times out or the pool is shutdown.
  83. destroyRawConnection: function destroyRawConnection(connection) {
  84. return _bluebird2.default.fromCallback(connection.end.bind(connection)).catch(function (err) {
  85. connection.__knex__disposed = err;
  86. }).finally(function () {
  87. return connection.removeAllListeners();
  88. });
  89. },
  90. validateConnection: function validateConnection(connection) {
  91. if (connection.state === 'connected' || connection.state === 'authenticated') {
  92. return true;
  93. }
  94. return false;
  95. },
  96. // Grab a connection, run the query via the MySQL streaming interface,
  97. // and pass that through to the stream we've sent back to the client.
  98. _stream: function _stream(connection, obj, stream, options) {
  99. options = options || {};
  100. var queryOptions = (0, _assign3.default)({ sql: obj.sql }, obj.options);
  101. return new _bluebird2.default(function (resolver, rejecter) {
  102. stream.on('error', rejecter);
  103. stream.on('end', resolver);
  104. connection.query(queryOptions, obj.bindings).stream(options).pipe(stream);
  105. });
  106. },
  107. // Runs the query on the specified connection, providing the bindings
  108. // and any other necessary prep work.
  109. _query: function _query(connection, obj) {
  110. if (!obj || typeof obj === 'string') obj = { sql: obj };
  111. return new _bluebird2.default(function (resolver, rejecter) {
  112. if (!obj.sql) {
  113. resolver();
  114. return;
  115. }
  116. var queryOptions = (0, _assign3.default)({ sql: obj.sql }, obj.options);
  117. connection.query(queryOptions, obj.bindings, function (err, rows, fields) {
  118. if (err) return rejecter(err);
  119. obj.response = [rows, fields];
  120. resolver(obj);
  121. });
  122. });
  123. },
  124. // Process the response as returned from the query.
  125. processResponse: function processResponse(obj, runner) {
  126. if (obj == null) return;
  127. var response = obj.response;
  128. var method = obj.method;
  129. var rows = response[0];
  130. var fields = response[1];
  131. if (obj.output) return obj.output.call(runner, rows, fields);
  132. switch (method) {
  133. case 'select':
  134. case 'pluck':
  135. case 'first':
  136. {
  137. var resp = helpers.skim(rows);
  138. if (method === 'pluck') return (0, _map3.default)(resp, obj.pluck);
  139. return method === 'first' ? resp[0] : resp;
  140. }
  141. case 'insert':
  142. return [rows.insertId];
  143. case 'del':
  144. case 'update':
  145. case 'counter':
  146. return rows.affectedRows;
  147. default:
  148. return response;
  149. }
  150. },
  151. canCancelQuery: true,
  152. cancelQuery: function cancelQuery(connectionToKill) {
  153. var _this2 = this;
  154. var acquiringConn = this.acquireConnection();
  155. // Error out if we can't acquire connection in time.
  156. // Purposely not putting timeout on `KILL QUERY` execution because erroring
  157. // early there would release the `connectionToKill` back to the pool with
  158. // a `KILL QUERY` command yet to finish.
  159. return acquiringConn.timeout(100).then(function (conn) {
  160. return _this2.query(conn, {
  161. method: 'raw',
  162. sql: 'KILL QUERY ?',
  163. bindings: [connectionToKill.threadId],
  164. options: {}
  165. });
  166. }).finally(function () {
  167. // NOT returning this promise because we want to release the connection
  168. // in a non-blocking fashion
  169. acquiringConn.then(function (conn) {
  170. return _this2.releaseConnection(conn);
  171. });
  172. });
  173. }
  174. });
  175. exports.default = Client_MySQL;
  176. module.exports = exports['default'];