index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _keys = require('babel-runtime/core-js/object/keys');
  4. var _keys2 = _interopRequireDefault(_keys);
  5. var _map2 = require('lodash/map');
  6. var _map3 = _interopRequireDefault(_map2);
  7. var _assign2 = require('lodash/assign');
  8. var _assign3 = _interopRequireDefault(_assign2);
  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. function Client_MariaSQL(config) {
  22. _mysql2.default.call(this, config);
  23. }
  24. // MariaSQL Client
  25. // -------
  26. (0, _inherits2.default)(Client_MariaSQL, _mysql2.default);
  27. (0, _assign3.default)(Client_MariaSQL.prototype, {
  28. dialect: 'mariadb',
  29. driverName: 'mariasql',
  30. transaction: function transaction() {
  31. return new (Function.prototype.bind.apply(_transaction2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  32. },
  33. _driver: function _driver() {
  34. return require('mariasql');
  35. },
  36. // Get a raw connection, called by the `pool` whenever a new
  37. // connection needs to be added to the pool.
  38. acquireRawConnection: function acquireRawConnection() {
  39. var _this = this;
  40. return new _bluebird2.default(function (resolver, rejecter) {
  41. var connection = new _this.driver();
  42. connection.connect((0, _assign3.default)({ metadata: true }, _this.connectionSettings));
  43. connection.on('ready', function () {
  44. connection.removeAllListeners('error');
  45. resolver(connection);
  46. }).on('error', rejecter);
  47. });
  48. },
  49. validateConnection: function validateConnection(connection) {
  50. return connection.connected === true;
  51. },
  52. // Used to explicitly close a connection, called internally by the pool
  53. // when a connection times out or the pool is shutdown.
  54. destroyRawConnection: function destroyRawConnection(connection) {
  55. connection.removeAllListeners();
  56. connection.end();
  57. },
  58. // Return the database for the MariaSQL client.
  59. database: function database() {
  60. return this.connectionSettings.db;
  61. },
  62. // Grab a connection, run the query via the MariaSQL streaming interface,
  63. // and pass that through to the stream we've sent back to the client.
  64. _stream: function _stream(connection, sql, stream) {
  65. return new _bluebird2.default(function (resolver, rejecter) {
  66. connection.query(sql.sql, sql.bindings).on('result', function (res) {
  67. res.on('error', rejecter).on('end', function () {
  68. resolver(res.info);
  69. }).on('data', function (data) {
  70. stream.write(handleRow(data, res.info.metadata));
  71. });
  72. }).on('error', rejecter);
  73. });
  74. },
  75. // Runs the query on the specified connection, providing the bindings
  76. // and any other necessary prep work.
  77. _query: function _query(connection, obj) {
  78. var _this2 = this;
  79. var tz = this.connectionSettings.timezone || 'local';
  80. return new _bluebird2.default(function (resolver, rejecter) {
  81. if (!obj.sql) return resolver();
  82. var sql = _this2._formatQuery(obj.sql, obj.bindings, tz);
  83. connection.query(sql, function (err, rows) {
  84. if (err) {
  85. return rejecter(err);
  86. }
  87. handleRows(rows, rows.info.metadata);
  88. obj.response = [rows, rows.info];
  89. resolver(obj);
  90. });
  91. });
  92. },
  93. // Process the response as returned from the query.
  94. processResponse: function processResponse(obj, runner) {
  95. var response = obj.response;
  96. var method = obj.method;
  97. var rows = response[0];
  98. var data = response[1];
  99. if (obj.output) return obj.output.call(runner, rows /*, fields*/);
  100. switch (method) {
  101. case 'select':
  102. case 'pluck':
  103. case 'first':
  104. {
  105. var resp = helpers.skim(rows);
  106. if (method === 'pluck') return (0, _map3.default)(resp, obj.pluck);
  107. return method === 'first' ? resp[0] : resp;
  108. }
  109. case 'insert':
  110. return [data.insertId];
  111. case 'del':
  112. case 'update':
  113. case 'counter':
  114. return parseInt(data.affectedRows, 10);
  115. default:
  116. return response;
  117. }
  118. }
  119. });
  120. function parseType(value, type) {
  121. switch (type) {
  122. case 'DATETIME':
  123. case 'TIMESTAMP':
  124. return new Date(value);
  125. case 'INTEGER':
  126. return parseInt(value, 10);
  127. default:
  128. return value;
  129. }
  130. }
  131. function handleRow(row, metadata) {
  132. var keys = (0, _keys2.default)(metadata);
  133. for (var i = 0; i < keys.length; i++) {
  134. var key = keys[i];
  135. var type = metadata[key].type;
  136. row[key] = parseType(row[key], type);
  137. }
  138. return row;
  139. }
  140. function handleRows(rows, metadata) {
  141. for (var i = 0; i < rows.length; i++) {
  142. var row = rows[i];
  143. handleRow(row, metadata);
  144. }
  145. return rows;
  146. }
  147. exports.default = Client_MariaSQL;
  148. module.exports = exports['default'];