compiler.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _compact2 = require('lodash/compact');
  4. var _compact3 = _interopRequireDefault(_compact2);
  5. var _reduce2 = require('lodash/reduce');
  6. var _reduce3 = _interopRequireDefault(_reduce2);
  7. var _map2 = require('lodash/map');
  8. var _map3 = _interopRequireDefault(_map2);
  9. var _isString2 = require('lodash/isString');
  10. var _isString3 = _interopRequireDefault(_isString2);
  11. var _isEmpty2 = require('lodash/isEmpty');
  12. var _isEmpty3 = _interopRequireDefault(_isEmpty2);
  13. var _isPlainObject2 = require('lodash/isPlainObject');
  14. var _isPlainObject3 = _interopRequireDefault(_isPlainObject2);
  15. var _assign2 = require('lodash/assign');
  16. var _assign3 = _interopRequireDefault(_assign2);
  17. var _inherits = require('inherits');
  18. var _inherits2 = _interopRequireDefault(_inherits);
  19. var _compiler = require('../../../query/compiler');
  20. var _compiler2 = _interopRequireDefault(_compiler);
  21. var _helpers = require('../../../helpers');
  22. var helpers = _interopRequireWildcard(_helpers);
  23. var _utils = require('../utils');
  24. 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; } }
  25. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  26. /* eslint max-len:0 */
  27. // Oracle Query Builder & Compiler
  28. // ------
  29. var components = ['columns', 'join', 'where', 'union', 'group', 'having', 'order', 'lock'];
  30. // Query Compiler
  31. // -------
  32. // Set the "Formatter" to use for the queries,
  33. // ensuring that all parameterized values (even across sub-queries)
  34. // are properly built into the same query.
  35. function QueryCompiler_Oracle(client, builder) {
  36. _compiler2.default.call(this, client, builder);
  37. }
  38. (0, _inherits2.default)(QueryCompiler_Oracle, _compiler2.default);
  39. (0, _assign3.default)(QueryCompiler_Oracle.prototype, {
  40. // Compiles an "insert" query, allowing for multiple
  41. // inserts using a single query statement.
  42. insert: function insert() {
  43. var _this = this;
  44. var insertValues = this.single.insert || [];
  45. var returning = this.single.returning;
  46. if (!Array.isArray(insertValues) && (0, _isPlainObject3.default)(this.single.insert)) {
  47. insertValues = [this.single.insert];
  48. }
  49. // always wrap returning argument in array
  50. if (returning && !Array.isArray(returning)) {
  51. returning = [returning];
  52. }
  53. if (Array.isArray(insertValues) && insertValues.length === 1 && (0, _isEmpty3.default)(insertValues[0])) {
  54. return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' (' + this.formatter.wrap(this.single.returning) + ') values (default)', returning, this.tableName);
  55. }
  56. if ((0, _isEmpty3.default)(this.single.insert) && typeof this.single.insert !== 'function') {
  57. return '';
  58. }
  59. var insertData = this._prepInsert(insertValues);
  60. var sql = {};
  61. if ((0, _isString3.default)(insertData)) {
  62. return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' ' + insertData, returning);
  63. }
  64. if (insertData.values.length === 1) {
  65. return this._addReturningToSqlAndConvert('insert into ' + this.tableName + ' (' + this.formatter.columnize(insertData.columns) + ') values (' + this.formatter.parameterize(insertData.values[0]) + ')', returning, this.tableName);
  66. }
  67. var insertDefaultsOnly = insertData.columns.length === 0;
  68. sql.sql = 'begin ' + (0, _map3.default)(insertData.values, function (value) {
  69. var returningHelper = void 0;
  70. var parameterizedValues = !insertDefaultsOnly ? _this.formatter.parameterize(value, _this.client.valueForUndefined) : '';
  71. var returningValues = Array.isArray(returning) ? returning : [returning];
  72. var subSql = 'insert into ' + _this.tableName + ' ';
  73. if (returning) {
  74. returningHelper = new _utils.ReturningHelper(returningValues.join(':'));
  75. sql.outParams = (sql.outParams || []).concat(returningHelper);
  76. }
  77. if (insertDefaultsOnly) {
  78. // no columns given so only the default value
  79. subSql += '(' + _this.formatter.wrap(_this.single.returning) + ') values (default)';
  80. } else {
  81. subSql += '(' + _this.formatter.columnize(insertData.columns) + ') values (' + parameterizedValues + ')';
  82. }
  83. subSql += returning ? ' returning ROWID into ' + _this.formatter.parameter(returningHelper) : '';
  84. // pre bind position because subSql is an execute immediate parameter
  85. // later position binding will only convert the ? params
  86. subSql = _this.formatter.client.positionBindings(subSql);
  87. var parameterizedValuesWithoutDefault = parameterizedValues.replace('DEFAULT, ', '').replace(', DEFAULT', '');
  88. return 'execute immediate \'' + subSql.replace(/'/g, "''") + (parameterizedValuesWithoutDefault || returning ? '\' using ' : '') + parameterizedValuesWithoutDefault + (parameterizedValuesWithoutDefault && returning ? ', ' : '') + (returning ? 'out ?' : '') + ';';
  89. }).join(' ') + 'end;';
  90. if (returning) {
  91. sql.returning = returning;
  92. // generate select statement with special order by to keep the order because 'in (..)' may change the order
  93. sql.returningSql = 'select ' + this.formatter.columnize(returning) + ' from ' + this.tableName + ' where ROWID in (' + sql.outParams.map(function (v, i) {
  94. return ':' + (i + 1);
  95. }).join(', ') + ')' + ' order by case ROWID ' + sql.outParams.map(function (v, i) {
  96. return 'when CHARTOROWID(:' + (i + 1) + ') then ' + i;
  97. }).join(' ') + ' end';
  98. }
  99. return sql;
  100. },
  101. // Update method, including joins, wheres, order & limits.
  102. update: function update() {
  103. var updates = this._prepUpdate(this.single.update);
  104. var where = this.where();
  105. var returning = this.single.returning;
  106. var sql = 'update ' + this.tableName + ' set ' + updates.join(', ') + (where ? ' ' + where : '');
  107. if (!returning) {
  108. return sql;
  109. }
  110. // always wrap returning argument in array
  111. if (returning && !Array.isArray(returning)) {
  112. returning = [returning];
  113. }
  114. return this._addReturningToSqlAndConvert(sql, returning, this.tableName);
  115. },
  116. // Compiles a `truncate` query.
  117. truncate: function truncate() {
  118. return 'truncate table ' + this.tableName;
  119. },
  120. forUpdate: function forUpdate() {
  121. return 'for update';
  122. },
  123. forShare: function forShare() {
  124. // lock for share is not directly supported by oracle
  125. // use LOCK TABLE .. IN SHARE MODE; instead
  126. helpers.warn('lock for share is not supported by oracle dialect');
  127. return '';
  128. },
  129. // Compiles a `columnInfo` query.
  130. columnInfo: function columnInfo() {
  131. var column = this.single.columnInfo;
  132. // Node oracle drivers doesn't support LONG type (which is data_default type)
  133. var sql = 'select * from xmltable( \'/ROWSET/ROW\'\n passing dbms_xmlgen.getXMLType(\'\n select char_col_decl_length, column_name, data_type, data_default, nullable\n from user_tab_columns where table_name = \'\'' + this.single.table + '\'\' \')\n columns\n CHAR_COL_DECL_LENGTH number, COLUMN_NAME varchar2(200), DATA_TYPE varchar2(106),\n DATA_DEFAULT clob, NULLABLE varchar2(1))';
  134. return {
  135. sql: sql,
  136. output: function output(resp) {
  137. var out = (0, _reduce3.default)(resp, function (columns, val) {
  138. columns[val.COLUMN_NAME] = {
  139. type: val.DATA_TYPE,
  140. defaultValue: val.DATA_DEFAULT,
  141. maxLength: val.CHAR_COL_DECL_LENGTH,
  142. nullable: val.NULLABLE === 'Y'
  143. };
  144. return columns;
  145. }, {});
  146. return column && out[column] || out;
  147. }
  148. };
  149. },
  150. select: function select() {
  151. var _this2 = this;
  152. var query = this.with();
  153. var statements = (0, _map3.default)(components, function (component) {
  154. return _this2[component]();
  155. });
  156. query += (0, _compact3.default)(statements).join(' ');
  157. return this._surroundQueryWithLimitAndOffset(query);
  158. },
  159. aggregate: function aggregate(stmt) {
  160. var val = stmt.value;
  161. var splitOn = val.toLowerCase().indexOf(' as ');
  162. var distinct = stmt.aggregateDistinct ? 'distinct ' : '';
  163. // Allows us to speciy an alias for the aggregate types.
  164. if (splitOn !== -1) {
  165. var col = val.slice(0, splitOn);
  166. var alias = val.slice(splitOn + 4);
  167. return stmt.method + '(' + distinct + this.formatter.wrap(col) + ') ' + this.formatter.wrap(alias);
  168. }
  169. return stmt.method + '(' + distinct + this.formatter.wrap(val) + ')';
  170. },
  171. // for single commands only
  172. _addReturningToSqlAndConvert: function _addReturningToSqlAndConvert(sql, returning, tableName) {
  173. var res = {
  174. sql: sql
  175. };
  176. if (!returning) {
  177. return res;
  178. }
  179. var returningValues = Array.isArray(returning) ? returning : [returning];
  180. var returningHelper = new _utils.ReturningHelper(returningValues.join(':'));
  181. res.sql = sql + ' returning ROWID into ' + this.formatter.parameter(returningHelper);
  182. res.returningSql = 'select ' + this.formatter.columnize(returning) + ' from ' + tableName + ' where ROWID = :1';
  183. res.outParams = [returningHelper];
  184. res.returning = returning;
  185. return res;
  186. },
  187. _surroundQueryWithLimitAndOffset: function _surroundQueryWithLimitAndOffset(query) {
  188. var limit = this.single.limit;
  189. var offset = this.single.offset;
  190. var hasLimit = limit || limit === 0 || limit === '0';
  191. limit = +limit;
  192. if (!hasLimit && !offset) return query;
  193. query = query || "";
  194. if (hasLimit && !offset) {
  195. return 'select * from (' + query + ') where rownum <= ' + this.formatter.parameter(limit);
  196. }
  197. var endRow = +offset + (hasLimit ? limit : 10000000000000);
  198. return "select * from " + "(select row_.*, ROWNUM rownum_ from (" + query + ") row_ " + "where rownum <= " + this.formatter.parameter(endRow) + ") " + "where rownum_ > " + this.formatter.parameter(offset);
  199. }
  200. });
  201. // Compiles the `select` statement, or nested sub-selects
  202. // by calling each of the component compilers, trimming out
  203. // the empties, and returning a generated query string.
  204. QueryCompiler_Oracle.prototype.first = QueryCompiler_Oracle.prototype.select;
  205. exports.default = QueryCompiler_Oracle;
  206. module.exports = exports['default'];