compiler.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof2 = require('babel-runtime/helpers/typeof');
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _compact2 = require('lodash/compact');
  6. var _compact3 = _interopRequireDefault(_compact2);
  7. var _isEmpty2 = require('lodash/isEmpty');
  8. var _isEmpty3 = _interopRequireDefault(_isEmpty2);
  9. var _assign2 = require('lodash/assign');
  10. var _assign3 = _interopRequireDefault(_assign2);
  11. var _inherits = require('inherits');
  12. var _inherits2 = _interopRequireDefault(_inherits);
  13. var _compiler = require('../../../query/compiler');
  14. var _compiler2 = _interopRequireDefault(_compiler);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. // MSSQL Query Compiler
  17. // ------
  18. function QueryCompiler_MSSQL(client, builder) {
  19. _compiler2.default.call(this, client, builder);
  20. }
  21. (0, _inherits2.default)(QueryCompiler_MSSQL, _compiler2.default);
  22. var components = ['columns', 'join', 'lock', 'where', 'union', 'group', 'having', 'order', 'limit', 'offset'];
  23. (0, _assign3.default)(QueryCompiler_MSSQL.prototype, {
  24. _emptyInsertValue: 'default values',
  25. select: function select() {
  26. var _this = this;
  27. var sql = this.with();
  28. var statements = components.map(function (component) {
  29. return _this[component](_this);
  30. });
  31. return sql + (0, _compact3.default)(statements).join(' ');
  32. },
  33. // Compiles an "insert" query, allowing for multiple
  34. // inserts using a single query statement.
  35. insert: function insert() {
  36. var insertValues = this.single.insert || [];
  37. var sql = this.with() + ('insert into ' + this.tableName + ' ');
  38. var returning = this.single.returning;
  39. var returningSql = returning ? this._returning('insert', returning) + ' ' : '';
  40. if (Array.isArray(insertValues)) {
  41. if (insertValues.length === 0) {
  42. return '';
  43. }
  44. } else if ((typeof insertValues === 'undefined' ? 'undefined' : (0, _typeof3.default)(insertValues)) === 'object' && (0, _isEmpty3.default)(insertValues)) {
  45. return {
  46. sql: sql + returningSql + this._emptyInsertValue,
  47. returning: returning
  48. };
  49. }
  50. var insertData = this._prepInsert(insertValues);
  51. if (typeof insertData === 'string') {
  52. sql += insertData;
  53. } else {
  54. if (insertData.columns.length) {
  55. sql += '(' + this.formatter.columnize(insertData.columns);
  56. sql += ') ' + returningSql + 'values (';
  57. var i = -1;
  58. while (++i < insertData.values.length) {
  59. if (i !== 0) sql += '), (';
  60. sql += this.formatter.parameterize(insertData.values[i], this.client.valueForUndefined);
  61. }
  62. sql += ')';
  63. } else if (insertValues.length === 1 && insertValues[0]) {
  64. sql += returningSql + this._emptyInsertValue;
  65. } else {
  66. sql = '';
  67. }
  68. }
  69. return {
  70. sql: sql,
  71. returning: returning
  72. };
  73. },
  74. // Compiles an `update` query, allowing for a return value.
  75. update: function update() {
  76. var updates = this._prepUpdate(this.single.update);
  77. var join = this.join();
  78. var where = this.where();
  79. var order = this.order();
  80. var top = this.top();
  81. var returning = this.single.returning;
  82. return {
  83. sql: this.with() + ('update ' + (top ? top + ' ' : '') + this.tableName) + ' set ' + updates.join(', ') + (join ? ' from ' + this.tableName + ' ' + join : '') + (returning ? ' ' + this._returning('update', returning) : '') + (where ? ' ' + where : '') + (order ? ' ' + order : '') + (!returning ? this._returning('rowcount', '@@rowcount') : ''),
  84. returning: returning || '@@rowcount'
  85. };
  86. },
  87. // Compiles a `delete` query.
  88. del: function del() {
  89. // Make sure tableName is processed by the formatter first.
  90. var tableName = this.tableName;
  91. var wheres = this.where();
  92. var returning = this.single.returning;
  93. return {
  94. sql: this.with() + ('delete from ' + tableName) + (returning ? ' ' + this._returning('del', returning) : '') + (wheres ? ' ' + wheres : '') + (!returning ? this._returning('rowcount', '@@rowcount') : ''),
  95. returning: returning || '@@rowcount'
  96. };
  97. },
  98. // Compiles the columns in the query, specifying if an item was distinct.
  99. columns: function columns() {
  100. var distinct = false;
  101. if (this.onlyUnions()) return '';
  102. var columns = this.grouped.columns || [];
  103. var i = -1,
  104. sql = [];
  105. if (columns) {
  106. while (++i < columns.length) {
  107. var stmt = columns[i];
  108. if (stmt.distinct) distinct = true;
  109. if (stmt.type === 'aggregate') {
  110. sql.push(this.aggregate(stmt));
  111. } else if (stmt.value && stmt.value.length > 0) {
  112. sql.push(this.formatter.columnize(stmt.value));
  113. }
  114. }
  115. }
  116. if (sql.length === 0) sql = ['*'];
  117. var top = this.top();
  118. return 'select ' + (distinct ? 'distinct ' : '') + (top ? top + ' ' : '') + sql.join(', ') + (this.tableName ? ' from ' + this.tableName : '');
  119. },
  120. _returning: function _returning(method, value) {
  121. switch (method) {
  122. case 'update':
  123. case 'insert':
  124. return value ? 'output ' + this.formatter.columnizeWithPrefix('inserted.', value) : '';
  125. case 'del':
  126. return value ? 'output ' + this.formatter.columnizeWithPrefix('deleted.', value) : '';
  127. case 'rowcount':
  128. return value ? ';select @@rowcount' : '';
  129. }
  130. },
  131. // Compiles a `truncate` query.
  132. truncate: function truncate() {
  133. return 'truncate table ' + this.tableName;
  134. },
  135. forUpdate: function forUpdate() {
  136. return 'with (READCOMMITTEDLOCK)';
  137. },
  138. forShare: function forShare() {
  139. return 'with (NOLOCK)';
  140. },
  141. // Compiles a `columnInfo` query.
  142. columnInfo: function columnInfo() {
  143. var column = this.single.columnInfo;
  144. var sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
  145. var bindings = [this.single.table, this.client.database()];
  146. if (this.single.schema) {
  147. sql += ' and table_schema = ?';
  148. bindings.push(this.single.schema);
  149. } else {
  150. sql += ' and table_schema = \'dbo\'';
  151. }
  152. return {
  153. sql: sql,
  154. bindings: bindings,
  155. output: function output(resp) {
  156. var out = resp.reduce(function (columns, val) {
  157. columns[val.COLUMN_NAME] = {
  158. defaultValue: val.COLUMN_DEFAULT,
  159. type: val.DATA_TYPE,
  160. maxLength: val.CHARACTER_MAXIMUM_LENGTH,
  161. nullable: val.IS_NULLABLE === 'YES'
  162. };
  163. return columns;
  164. }, {});
  165. return column && out[column] || out;
  166. }
  167. };
  168. },
  169. top: function top() {
  170. var noLimit = !this.single.limit && this.single.limit !== 0;
  171. var noOffset = !this.single.offset;
  172. if (noLimit || !noOffset) return '';
  173. return 'top (' + this.formatter.parameter(this.single.limit) + ')';
  174. },
  175. limit: function limit() {
  176. return '';
  177. },
  178. offset: function offset() {
  179. var noLimit = !this.single.limit && this.single.limit !== 0;
  180. var noOffset = !this.single.offset;
  181. if (noOffset) return '';
  182. var offset = 'offset ' + (noOffset ? '0' : this.formatter.parameter(this.single.offset)) + ' rows';
  183. if (!noLimit) {
  184. offset += ' fetch next ' + this.formatter.parameter(this.single.limit) + ' rows only';
  185. }
  186. return offset;
  187. }
  188. });
  189. // Set the QueryBuilder & QueryCompiler on the client object,
  190. // in case anyone wants to modify things to suit their own purposes.
  191. exports.default = QueryCompiler_MSSQL;
  192. module.exports = exports['default'];