index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _values2 = require('lodash/values');
  4. var _values3 = _interopRequireDefault(_values2);
  5. var _flatten2 = require('lodash/flatten');
  6. var _flatten3 = _interopRequireDefault(_flatten2);
  7. var _map2 = require('lodash/map');
  8. var _map3 = _interopRequireDefault(_map2);
  9. var _assign2 = require('lodash/assign');
  10. var _assign3 = _interopRequireDefault(_assign2);
  11. exports.default = Client_Oracle;
  12. var _inherits = require('inherits');
  13. var _inherits2 = _interopRequireDefault(_inherits);
  14. var _client = require('../../client');
  15. var _client2 = _interopRequireDefault(_client);
  16. var _bluebird = require('bluebird');
  17. var _bluebird2 = _interopRequireDefault(_bluebird);
  18. var _helpers = require('../../helpers');
  19. var helpers = _interopRequireWildcard(_helpers);
  20. var _string = require('../../query/string');
  21. var _formatter = require('./formatter');
  22. var _formatter2 = _interopRequireDefault(_formatter);
  23. var _transaction = require('./transaction');
  24. var _transaction2 = _interopRequireDefault(_transaction);
  25. var _compiler = require('./query/compiler');
  26. var _compiler2 = _interopRequireDefault(_compiler);
  27. var _compiler3 = require('./schema/compiler');
  28. var _compiler4 = _interopRequireDefault(_compiler3);
  29. var _columnbuilder = require('./schema/columnbuilder');
  30. var _columnbuilder2 = _interopRequireDefault(_columnbuilder);
  31. var _columncompiler = require('./schema/columncompiler');
  32. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  33. var _tablecompiler = require('./schema/tablecompiler');
  34. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  35. var _utils = require('./utils');
  36. 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; } }
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. // Always initialize with the "QueryBuilder" and "QueryCompiler"
  39. // objects, which extend the base 'lib/query/builder' and
  40. // 'lib/query/compiler', respectively.
  41. function Client_Oracle(config) {
  42. _client2.default.call(this, config);
  43. }
  44. // Oracle Client
  45. // -------
  46. (0, _inherits2.default)(Client_Oracle, _client2.default);
  47. (0, _assign3.default)(Client_Oracle.prototype, {
  48. dialect: 'oracle',
  49. driverName: 'oracle',
  50. _driver: function _driver() {
  51. return require('oracle');
  52. },
  53. transaction: function transaction() {
  54. return new (Function.prototype.bind.apply(_transaction2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  55. },
  56. formatter: function formatter() {
  57. return new _formatter2.default(this);
  58. },
  59. queryCompiler: function queryCompiler() {
  60. return new (Function.prototype.bind.apply(_compiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  61. },
  62. schemaCompiler: function schemaCompiler() {
  63. return new (Function.prototype.bind.apply(_compiler4.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  64. },
  65. columnBuilder: function columnBuilder() {
  66. return new (Function.prototype.bind.apply(_columnbuilder2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  67. },
  68. columnCompiler: function columnCompiler() {
  69. return new (Function.prototype.bind.apply(_columncompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  70. },
  71. tableCompiler: function tableCompiler() {
  72. return new (Function.prototype.bind.apply(_tablecompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  73. },
  74. prepBindings: function prepBindings(bindings) {
  75. var _this = this;
  76. return (0, _map3.default)(bindings, function (value) {
  77. // returning helper uses always ROWID as string
  78. if (value instanceof _utils.ReturningHelper && _this.driver) {
  79. return new _this.driver.OutParam(_this.driver.OCCISTRING);
  80. } else if (typeof value === 'boolean') {
  81. return value ? 1 : 0;
  82. } else if (Buffer.isBuffer(value)) {
  83. return (0, _string.bufferToString)(value);
  84. }
  85. return value;
  86. });
  87. },
  88. // Get a raw connection, called by the `pool` whenever a new
  89. // connection needs to be added to the pool.
  90. acquireRawConnection: function acquireRawConnection() {
  91. var _this2 = this;
  92. return new _bluebird2.default(function (resolver, rejecter) {
  93. _this2.driver.connect(_this2.connectionSettings, function (err, connection) {
  94. if (err) return rejecter(err);
  95. _bluebird2.default.promisifyAll(connection);
  96. if (_this2.connectionSettings.prefetchRowCount) {
  97. connection.setPrefetchRowCount(_this2.connectionSettings.prefetchRowCount);
  98. }
  99. resolver(connection);
  100. });
  101. });
  102. },
  103. // Used to explicitly close a connection, called internally by the pool
  104. // when a connection times out or the pool is shutdown.
  105. destroyRawConnection: function destroyRawConnection(connection) {
  106. connection.close();
  107. },
  108. // Return the database for the Oracle client.
  109. database: function database() {
  110. return this.connectionSettings.database;
  111. },
  112. // Position the bindings for the query.
  113. positionBindings: function positionBindings(sql) {
  114. var questionCount = 0;
  115. return sql.replace(/\?/g, function () {
  116. questionCount += 1;
  117. return ':' + questionCount;
  118. });
  119. },
  120. _stream: function _stream(connection, obj, stream, options) {
  121. obj.sql = this.positionBindings(obj.sql);
  122. return new _bluebird2.default(function (resolver, rejecter) {
  123. stream.on('error', function (err) {
  124. if (isConnectionError(err)) {
  125. connection.__knex__disposed = err;
  126. }
  127. rejecter(err);
  128. });
  129. stream.on('end', resolver);
  130. var queryStream = connection.queryStream(obj.sql, obj.bindings, options);
  131. queryStream.pipe(stream);
  132. });
  133. },
  134. // Runs the query on the specified connection, providing the bindings
  135. // and any other necessary prep work.
  136. _query: function _query(connection, obj) {
  137. // convert ? params into positional bindings (:1)
  138. obj.sql = this.positionBindings(obj.sql);
  139. if (!obj.sql) throw new Error('The query is empty');
  140. return connection.executeAsync(obj.sql, obj.bindings).then(function (response) {
  141. if (!obj.returning) return response;
  142. var rowIds = obj.outParams.map(function (v, i) {
  143. return response['returnParam' + (i ? i : '')];
  144. });
  145. return connection.executeAsync(obj.returningSql, rowIds);
  146. }).then(function (response) {
  147. obj.response = response;
  148. obj.rowsAffected = response.updateCount;
  149. return obj;
  150. }).catch(function (err) {
  151. if (isConnectionError(err)) {
  152. connection.__knex__disposed = err;
  153. }
  154. throw err;
  155. });
  156. },
  157. // Process the response as returned from the query.
  158. processResponse: function processResponse(obj, runner) {
  159. var response = obj.response;
  160. var method = obj.method;
  161. if (obj.output) return obj.output.call(runner, response);
  162. switch (method) {
  163. case 'select':
  164. case 'pluck':
  165. case 'first':
  166. response = helpers.skim(response);
  167. if (obj.method === 'pluck') response = (0, _map3.default)(response, obj.pluck);
  168. return obj.method === 'first' ? response[0] : response;
  169. case 'insert':
  170. case 'del':
  171. case 'update':
  172. case 'counter':
  173. if (obj.returning) {
  174. if (obj.returning.length > 1 || obj.returning[0] === '*') {
  175. return response;
  176. }
  177. // return an array with values if only one returning value was specified
  178. return (0, _flatten3.default)((0, _map3.default)(response, _values3.default));
  179. }
  180. return obj.rowsAffected;
  181. default:
  182. return response;
  183. }
  184. }
  185. });
  186. // If the error is any of these, we'll assume we need to
  187. // mark the connection as failed
  188. var connectionErrors = ['ORA-12514', 'NJS-040', 'NJS-024', 'NJS-003', 'NJS-024'];
  189. function isConnectionError(err) {
  190. return connectionErrors.some(function (prefix) {
  191. return err.message.indexOf(prefix) === 0;
  192. });
  193. }
  194. module.exports = exports['default'];