compiler.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof2 = require('babel-runtime/helpers/typeof');
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _identity2 = require('lodash/identity');
  6. var _identity3 = _interopRequireDefault(_identity2);
  7. var _reduce2 = require('lodash/reduce');
  8. var _reduce3 = _interopRequireDefault(_reduce2);
  9. var _noop2 = require('lodash/noop');
  10. var _noop3 = _interopRequireDefault(_noop2);
  11. var _isString2 = require('lodash/isString');
  12. var _isString3 = _interopRequireDefault(_isString2);
  13. var _isEmpty2 = require('lodash/isEmpty');
  14. var _isEmpty3 = _interopRequireDefault(_isEmpty2);
  15. var _each2 = require('lodash/each');
  16. var _each3 = _interopRequireDefault(_each2);
  17. var _assign2 = require('lodash/assign');
  18. var _assign3 = _interopRequireDefault(_assign2);
  19. var _inherits = require('inherits');
  20. var _inherits2 = _interopRequireDefault(_inherits);
  21. var _compiler = require('../../../query/compiler');
  22. var _compiler2 = _interopRequireDefault(_compiler);
  23. var _helpers = require('../../../helpers');
  24. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  25. function QueryCompiler_SQLite3(client, builder) {
  26. _compiler2.default.call(this, client, builder);
  27. var returning = this.single.returning;
  28. if (returning) {
  29. (0, _helpers.warn)('.returning() is not supported by sqlite3 and will not have any effect.');
  30. }
  31. }
  32. // SQLite3 Query Builder & Compiler
  33. (0, _inherits2.default)(QueryCompiler_SQLite3, _compiler2.default);
  34. (0, _assign3.default)(QueryCompiler_SQLite3.prototype, {
  35. // The locks are not applicable in SQLite3
  36. forShare: emptyStr,
  37. forUpdate: emptyStr,
  38. // SQLite requires us to build the multi-row insert as a listing of select with
  39. // unions joining them together. So we'll build out this list of columns and
  40. // then join them all together with select unions to complete the queries.
  41. insert: function insert() {
  42. var insertValues = this.single.insert || [];
  43. var sql = this.with() + ('insert into ' + this.tableName + ' ');
  44. if (Array.isArray(insertValues)) {
  45. if (insertValues.length === 0) {
  46. return '';
  47. } else if (insertValues.length === 1 && insertValues[0] && (0, _isEmpty3.default)(insertValues[0])) {
  48. return sql + this._emptyInsertValue;
  49. }
  50. } else if ((typeof insertValues === 'undefined' ? 'undefined' : (0, _typeof3.default)(insertValues)) === 'object' && (0, _isEmpty3.default)(insertValues)) {
  51. return sql + this._emptyInsertValue;
  52. }
  53. var insertData = this._prepInsert(insertValues);
  54. if ((0, _isString3.default)(insertData)) {
  55. return sql + insertData;
  56. }
  57. if (insertData.columns.length === 0) {
  58. return '';
  59. }
  60. sql += '(' + this.formatter.columnize(insertData.columns) + ')';
  61. // backwards compatible error
  62. if (this.client.valueForUndefined !== null) {
  63. (0, _each3.default)(insertData.values, function (bindings) {
  64. (0, _each3.default)(bindings, function (binding) {
  65. if (binding === undefined) throw new TypeError('`sqlite` does not support inserting default values. Specify ' + 'values explicitly or use the `useNullAsDefault` config flag. ' + '(see docs http://knexjs.org/#Builder-insert).');
  66. });
  67. });
  68. }
  69. if (insertData.values.length === 1) {
  70. var parameters = this.formatter.parameterize(insertData.values[0], this.client.valueForUndefined);
  71. return sql + (' values (' + parameters + ')');
  72. }
  73. var blocks = [];
  74. var i = -1;
  75. while (++i < insertData.values.length) {
  76. var i2 = -1;
  77. var block = blocks[i] = [];
  78. var current = insertData.values[i];
  79. current = current === undefined ? this.client.valueForUndefined : current;
  80. while (++i2 < insertData.columns.length) {
  81. block.push(this.formatter.alias(this.formatter.parameter(current[i2]), this.formatter.wrap(insertData.columns[i2])));
  82. }
  83. blocks[i] = block.join(', ');
  84. }
  85. return sql + ' select ' + blocks.join(' union all select ');
  86. },
  87. // Compile a truncate table statement into SQL.
  88. truncate: function truncate() {
  89. var table = this.single.table;
  90. return {
  91. sql: 'delete from ' + this.tableName,
  92. output: function output() {
  93. return this.query({
  94. sql: 'delete from sqlite_sequence where name = \'' + table + '\''
  95. }).catch(_noop3.default);
  96. }
  97. };
  98. },
  99. // Compiles a `columnInfo` query
  100. columnInfo: function columnInfo() {
  101. var column = this.single.columnInfo;
  102. // The user may have specified a custom wrapIdentifier function in the config. We
  103. // need to run the identifiers through that function, but not format them as
  104. // identifiers otherwise.
  105. var table = this.client.customWrapIdentifier(this.single.table, _identity3.default);
  106. return {
  107. sql: 'PRAGMA table_info(`' + table + '`)',
  108. output: function output(resp) {
  109. var maxLengthRegex = /.*\((\d+)\)/;
  110. var out = (0, _reduce3.default)(resp, function (columns, val) {
  111. var type = val.type;
  112. var maxLength = type.match(maxLengthRegex);
  113. if (maxLength) {
  114. maxLength = maxLength[1];
  115. }
  116. type = maxLength ? type.split('(')[0] : type;
  117. columns[val.name] = {
  118. type: type.toLowerCase(),
  119. maxLength: maxLength,
  120. nullable: !val.notnull,
  121. defaultValue: val.dflt_value
  122. };
  123. return columns;
  124. }, {});
  125. return column && out[column] || out;
  126. }
  127. };
  128. },
  129. limit: function limit() {
  130. var noLimit = !this.single.limit && this.single.limit !== 0;
  131. if (noLimit && !this.single.offset) return '';
  132. // Workaround for offset only,
  133. // see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
  134. return 'limit ' + this.formatter.parameter(noLimit ? -1 : this.single.limit);
  135. }
  136. });
  137. function emptyStr() {
  138. return '';
  139. }
  140. exports.default = QueryCompiler_SQLite3;
  141. module.exports = exports['default'];