formatter.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  4. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  5. var _transform2 = require('lodash/transform');
  6. var _transform3 = _interopRequireDefault(_transform2);
  7. var _builder = require('./query/builder');
  8. var _builder2 = _interopRequireDefault(_builder);
  9. var _raw = require('./raw');
  10. var _raw2 = _interopRequireDefault(_raw);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. // Valid values for the `order by` clause generation.
  13. var orderBys = ['asc', 'desc'];
  14. // Turn this into a lookup map
  15. var operators = (0, _transform3.default)(['=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike', '&', '|', '^', '<<', '>>', 'rlike', 'regexp', 'not regexp', '~', '~*', '!~', '!~*', '#', '&&', '@>', '<@', '||'], function (result, key) {
  16. result[key] = true;
  17. }, {});
  18. var Formatter = function () {
  19. function Formatter(client) {
  20. (0, _classCallCheck3.default)(this, Formatter);
  21. this.client = client;
  22. this.bindings = [];
  23. }
  24. // Accepts a string or array of columns to wrap as appropriate.
  25. Formatter.prototype.columnize = function columnize(target) {
  26. var columns = typeof target === 'string' ? [target] : target;
  27. var str = '',
  28. i = -1;
  29. while (++i < columns.length) {
  30. if (i > 0) str += ', ';
  31. str += this.wrap(columns[i]);
  32. }
  33. return str;
  34. };
  35. // Turns a list of values into a list of ?'s, joining them with commas unless
  36. // a "joining" value is specified (e.g. ' and ')
  37. Formatter.prototype.parameterize = function parameterize(values, notSetValue) {
  38. if (typeof values === 'function') return this.parameter(values);
  39. values = Array.isArray(values) ? values : [values];
  40. var str = '',
  41. i = -1;
  42. while (++i < values.length) {
  43. if (i > 0) str += ', ';
  44. str += this.parameter(values[i] === undefined ? notSetValue : values[i]);
  45. }
  46. return str;
  47. };
  48. // Checks whether a value is a function... if it is, we compile it
  49. // otherwise we check whether it's a raw
  50. Formatter.prototype.parameter = function parameter(value) {
  51. if (typeof value === 'function') {
  52. return this.outputQuery(this.compileCallback(value), true);
  53. }
  54. return this.unwrapRaw(value, true) || '?';
  55. };
  56. Formatter.prototype.unwrapRaw = function unwrapRaw(value, isParameter) {
  57. var query = void 0;
  58. if (value instanceof _builder2.default) {
  59. query = this.client.queryCompiler(value).toSQL();
  60. if (query.bindings) {
  61. this.bindings = this.bindings.concat(query.bindings);
  62. }
  63. return this.outputQuery(query, isParameter);
  64. }
  65. if (value instanceof _raw2.default) {
  66. value.client = this.client;
  67. query = value.toSQL();
  68. if (query.bindings) {
  69. this.bindings = this.bindings.concat(query.bindings);
  70. }
  71. return query.sql;
  72. }
  73. if (isParameter) {
  74. this.bindings.push(value);
  75. }
  76. };
  77. Formatter.prototype.rawOrFn = function rawOrFn(value, method) {
  78. if (typeof value === 'function') {
  79. return this.outputQuery(this.compileCallback(value, method));
  80. }
  81. return this.unwrapRaw(value) || '';
  82. };
  83. // Puts the appropriate wrapper around a value depending on the database
  84. // engine, unless it's a knex.raw value, in which case it's left alone.
  85. Formatter.prototype.wrap = function wrap(value) {
  86. if (typeof value === 'function') {
  87. return this.outputQuery(this.compileCallback(value), true);
  88. }
  89. var raw = this.unwrapRaw(value);
  90. if (raw) return raw;
  91. if (typeof value === 'number') return value;
  92. return this._wrapString(value + '');
  93. };
  94. Formatter.prototype.wrapAsIdentifier = function wrapAsIdentifier(value) {
  95. return this.client.wrapIdentifier((value || '').trim());
  96. };
  97. Formatter.prototype.alias = function alias(first, second) {
  98. return first + ' as ' + second;
  99. };
  100. // The operator method takes a value and returns something or other.
  101. Formatter.prototype.operator = function operator(value) {
  102. var raw = this.unwrapRaw(value);
  103. if (raw) return raw;
  104. if (operators[(value || '').toLowerCase()] !== true) {
  105. throw new TypeError('The operator "' + value + '" is not permitted');
  106. }
  107. return value;
  108. };
  109. // Specify the direction of the ordering.
  110. Formatter.prototype.direction = function direction(value) {
  111. var raw = this.unwrapRaw(value);
  112. if (raw) return raw;
  113. return orderBys.indexOf((value || '').toLowerCase()) !== -1 ? value : 'asc';
  114. };
  115. // Compiles a callback using the query builder.
  116. Formatter.prototype.compileCallback = function compileCallback(callback, method) {
  117. var client = this.client;
  118. // Build the callback
  119. var builder = client.queryBuilder();
  120. callback.call(builder, builder);
  121. // Compile the callback, using the current formatter (to track all bindings).
  122. var compiler = client.queryCompiler(builder);
  123. compiler.formatter = this;
  124. // Return the compiled & parameterized sql.
  125. return compiler.toSQL(method || 'select');
  126. };
  127. // Ensures the query is aliased if necessary.
  128. Formatter.prototype.outputQuery = function outputQuery(compiled, isParameter) {
  129. var sql = compiled.sql || '';
  130. if (sql) {
  131. if ((compiled.method === 'select' || compiled.method === 'first') && (isParameter || compiled.as)) {
  132. sql = '(' + sql + ')';
  133. if (compiled.as) return this.alias(sql, this.wrap(compiled.as));
  134. }
  135. }
  136. return sql;
  137. };
  138. // Coerce to string to prevent strange errors when it's not a string.
  139. Formatter.prototype._wrapString = function _wrapString(value) {
  140. var asIndex = value.toLowerCase().indexOf(' as ');
  141. if (asIndex !== -1) {
  142. var first = value.slice(0, asIndex);
  143. var second = value.slice(asIndex + 4);
  144. return this.alias(this.wrap(first), this.wrapAsIdentifier(second));
  145. }
  146. var wrapped = [];
  147. var i = -1;
  148. var segments = value.split('.');
  149. while (++i < segments.length) {
  150. value = segments[i];
  151. if (i === 0 && segments.length > 1) {
  152. wrapped.push(this.wrap((value || '').trim()));
  153. } else {
  154. wrapped.push(this.client.wrapIdentifier((value || '').trim()));
  155. }
  156. }
  157. return wrapped.join('.');
  158. };
  159. return Formatter;
  160. }();
  161. exports.default = Formatter;
  162. module.exports = exports['default'];