index.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  4. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  5. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  6. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  7. var _inherits2 = require('babel-runtime/helpers/inherits');
  8. var _inherits3 = _interopRequireDefault(_inherits2);
  9. var _values2 = require('lodash/values');
  10. var _values3 = _interopRequireDefault(_values2);
  11. var _flatten2 = require('lodash/flatten');
  12. var _flatten3 = _interopRequireDefault(_flatten2);
  13. var _map2 = require('lodash/map');
  14. var _map3 = _interopRequireDefault(_map2);
  15. var _assign2 = require('lodash/assign');
  16. var _assign3 = _interopRequireDefault(_assign2);
  17. var _inherits4 = require('inherits');
  18. var _inherits5 = _interopRequireDefault(_inherits4);
  19. var _client = require('../../client');
  20. var _client2 = _interopRequireDefault(_client);
  21. var _bluebird = require('bluebird');
  22. var _bluebird2 = _interopRequireDefault(_bluebird);
  23. var _helpers = require('../../helpers');
  24. var helpers = _interopRequireWildcard(_helpers);
  25. var _formatter = require('../../formatter');
  26. var _formatter2 = _interopRequireDefault(_formatter);
  27. var _transaction = require('./transaction');
  28. var _transaction2 = _interopRequireDefault(_transaction);
  29. var _compiler = require('./query/compiler');
  30. var _compiler2 = _interopRequireDefault(_compiler);
  31. var _compiler3 = require('./schema/compiler');
  32. var _compiler4 = _interopRequireDefault(_compiler3);
  33. var _tablecompiler = require('./schema/tablecompiler');
  34. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  35. var _columncompiler = require('./schema/columncompiler');
  36. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  37. 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; } }
  38. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  39. // MSSQL Client
  40. // -------
  41. var isArray = Array.isArray;
  42. var SQL_INT4 = { MIN: -2147483648, MAX: 2147483647 };
  43. var SQL_BIGINT_SAFE = { MIN: -9007199254740991, MAX: 9007199254740991 };
  44. // Always initialize with the "QueryBuilder" and "QueryCompiler" objects, which
  45. // extend the base 'lib/query/builder' and 'lib/query/compiler', respectively.
  46. function Client_MSSQL(config) {
  47. // #1235 mssql module wants 'server', not 'host'. This is to enforce the same
  48. // options object across all dialects.
  49. if (config && config.connection && config.connection.host) {
  50. config.connection.server = config.connection.host;
  51. }
  52. _client2.default.call(this, config);
  53. }
  54. (0, _inherits5.default)(Client_MSSQL, _client2.default);
  55. (0, _assign3.default)(Client_MSSQL.prototype, {
  56. dialect: 'mssql',
  57. driverName: 'mssql',
  58. _driver: function _driver() {
  59. return require('mssql');
  60. },
  61. formatter: function formatter() {
  62. return new MSSQL_Formatter(this);
  63. },
  64. transaction: function transaction() {
  65. return new (Function.prototype.bind.apply(_transaction2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  66. },
  67. queryCompiler: function queryCompiler() {
  68. return new (Function.prototype.bind.apply(_compiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  69. },
  70. schemaCompiler: function schemaCompiler() {
  71. return new (Function.prototype.bind.apply(_compiler4.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  72. },
  73. tableCompiler: function tableCompiler() {
  74. return new (Function.prototype.bind.apply(_tablecompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  75. },
  76. columnCompiler: function columnCompiler() {
  77. return new (Function.prototype.bind.apply(_columncompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  78. },
  79. wrapIdentifier: function wrapIdentifier(value) {
  80. return value !== '*' ? '[' + value.replace(/\[/g, '\[') + ']' : '*';
  81. },
  82. // Get a raw connection, called by the `pool` whenever a new
  83. // connection needs to be added to the pool.
  84. acquireRawConnection: function acquireRawConnection() {
  85. var _this = this;
  86. return new _bluebird2.default(function (resolver, rejecter) {
  87. var connection = new _this.driver.Connection(_this.connectionSettings);
  88. connection.connect(function (err) {
  89. if (err) {
  90. return rejecter(err);
  91. }
  92. connection.on('error', function (err) {
  93. connection.__knex__disposed = err;
  94. });
  95. resolver(connection);
  96. });
  97. });
  98. },
  99. validateConnection: function validateConnection(connection) {
  100. return connection.connected === true;
  101. },
  102. // Used to explicitly close a connection, called internally by the pool
  103. // when a connection times out or the pool is shutdown.
  104. destroyRawConnection: function destroyRawConnection(connection) {
  105. connection.close();
  106. },
  107. // Position the bindings for the query.
  108. positionBindings: function positionBindings(sql) {
  109. var questionCount = -1;
  110. return sql.replace(/\?/g, function () {
  111. questionCount += 1;
  112. return '@p' + questionCount;
  113. });
  114. },
  115. // Grab a connection, run the query via the MSSQL streaming interface,
  116. // and pass that through to the stream we've sent back to the client.
  117. _stream: function _stream(connection, obj, stream, options) {
  118. var _this2 = this;
  119. options = options || {};
  120. if (!obj || typeof obj === 'string') obj = { sql: obj };
  121. // convert ? params into positional bindings (@p1)
  122. obj.sql = this.positionBindings(obj.sql);
  123. return new _bluebird2.default(function (resolver, rejecter) {
  124. stream.on('error', function (err) {
  125. rejecter(err);
  126. });
  127. stream.on('end', resolver);
  128. var _obj = obj,
  129. sql = _obj.sql;
  130. if (!sql) return resolver();
  131. var req = (connection.tx_ || connection).request();
  132. //req.verbose = true;
  133. req.multiple = true;
  134. req.stream = true;
  135. if (obj.bindings) {
  136. for (var i = 0; i < obj.bindings.length; i++) {
  137. _this2._setReqInput(req, i, obj.bindings[i]);
  138. }
  139. }
  140. req.pipe(stream);
  141. req.query(sql);
  142. });
  143. },
  144. // Runs the query on the specified connection, providing the bindings
  145. // and any other necessary prep work.
  146. _query: function _query(connection, obj) {
  147. var client = this;
  148. if (!obj || typeof obj === 'string') obj = { sql: obj };
  149. // convert ? params into positional bindings (@p1)
  150. obj.sql = this.positionBindings(obj.sql);
  151. return new _bluebird2.default(function (resolver, rejecter) {
  152. var _obj2 = obj,
  153. sql = _obj2.sql;
  154. if (!sql) return resolver();
  155. var req = (connection.tx_ || connection).request();
  156. // req.verbose = true;
  157. req.multiple = true;
  158. if (obj.bindings) {
  159. for (var i = 0; i < obj.bindings.length; i++) {
  160. client._setReqInput(req, i, obj.bindings[i]);
  161. }
  162. }
  163. req.query(sql, function (err, recordset) {
  164. if (err) {
  165. return rejecter(err);
  166. }
  167. obj.response = recordset[0];
  168. resolver(obj);
  169. });
  170. });
  171. },
  172. // sets a request input parameter. Detects bigints and decimals and sets type appropriately.
  173. _setReqInput: function _setReqInput(req, i, binding) {
  174. if (typeof binding == 'number') {
  175. if (binding % 1 !== 0) {
  176. req.input('p' + i, this.driver.Decimal(38, 10), binding);
  177. } else if (binding < SQL_INT4.MIN || binding > SQL_INT4.MAX) {
  178. if (binding < SQL_BIGINT_SAFE.MIN || binding > SQL_BIGINT_SAFE.MAX) {
  179. throw new Error('Bigint must be safe integer or must be passed as string, saw ' + binding);
  180. }
  181. req.input('p' + i, this.driver.BigInt, binding);
  182. } else {
  183. req.input('p' + i, this.driver.Int, binding);
  184. }
  185. } else {
  186. req.input('p' + i, binding);
  187. }
  188. },
  189. // Process the response as returned from the query.
  190. processResponse: function processResponse(obj, runner) {
  191. if (obj == null) return;
  192. var response = obj.response;
  193. var method = obj.method;
  194. if (obj.output) return obj.output.call(runner, response);
  195. switch (method) {
  196. case 'select':
  197. case 'pluck':
  198. case 'first':
  199. response = helpers.skim(response);
  200. if (method === 'pluck') return (0, _map3.default)(response, obj.pluck);
  201. return method === 'first' ? response[0] : response;
  202. case 'insert':
  203. case 'del':
  204. case 'update':
  205. case 'counter':
  206. if (obj.returning) {
  207. if (obj.returning === '@@rowcount') {
  208. return response[0][''];
  209. }
  210. if (isArray(obj.returning) && obj.returning.length > 1 || obj.returning[0] === '*') {
  211. return response;
  212. }
  213. // return an array with values if only one returning value was specified
  214. return (0, _flatten3.default)((0, _map3.default)(response, _values3.default));
  215. }
  216. return response;
  217. default:
  218. return response;
  219. }
  220. }
  221. });
  222. var MSSQL_Formatter = function (_Formatter) {
  223. (0, _inherits3.default)(MSSQL_Formatter, _Formatter);
  224. function MSSQL_Formatter() {
  225. (0, _classCallCheck3.default)(this, MSSQL_Formatter);
  226. return (0, _possibleConstructorReturn3.default)(this, _Formatter.apply(this, arguments));
  227. }
  228. // Accepts a string or array of columns to wrap as appropriate.
  229. MSSQL_Formatter.prototype.columnizeWithPrefix = function columnizeWithPrefix(prefix, target) {
  230. var columns = typeof target === 'string' ? [target] : target;
  231. var str = '',
  232. i = -1;
  233. while (++i < columns.length) {
  234. if (i > 0) str += ', ';
  235. str += prefix + this.wrap(columns[i]);
  236. }
  237. return str;
  238. };
  239. return MSSQL_Formatter;
  240. }(_formatter2.default);
  241. exports.default = Client_MSSQL;
  242. module.exports = exports['default'];