compiler.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof2 = require('babel-runtime/helpers/typeof');
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _reduce2 = require('lodash/reduce');
  6. var _reduce3 = _interopRequireDefault(_reduce2);
  7. var _noop2 = require('lodash/noop');
  8. var _noop3 = _interopRequireDefault(_noop2);
  9. var _isString2 = require('lodash/isString');
  10. var _isString3 = _interopRequireDefault(_isString2);
  11. var _isEmpty2 = require('lodash/isEmpty');
  12. var _isEmpty3 = _interopRequireDefault(_isEmpty2);
  13. var _each2 = require('lodash/each');
  14. var _each3 = _interopRequireDefault(_each2);
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. // SQLite3 Query Builder & Compiler
  23. function QueryCompiler_SQLite3(client, builder) {
  24. _compiler2.default.call(this, client, builder);
  25. }
  26. (0, _inherits2.default)(QueryCompiler_SQLite3, _compiler2.default);
  27. (0, _assign3.default)(QueryCompiler_SQLite3.prototype, {
  28. // The locks are not applicable in SQLite3
  29. forShare: emptyStr,
  30. forUpdate: emptyStr,
  31. // SQLite requires us to build the multi-row insert as a listing of select with
  32. // unions joining them together. So we'll build out this list of columns and
  33. // then join them all together with select unions to complete the queries.
  34. insert: function insert() {
  35. var insertValues = this.single.insert || [];
  36. var sql = this.with() + ('insert into ' + this.tableName + ' ');
  37. if (Array.isArray(insertValues)) {
  38. if (insertValues.length === 0) {
  39. return '';
  40. } else if (insertValues.length === 1 && insertValues[0] && (0, _isEmpty3.default)(insertValues[0])) {
  41. return sql + this._emptyInsertValue;
  42. }
  43. } else if ((typeof insertValues === 'undefined' ? 'undefined' : (0, _typeof3.default)(insertValues)) === 'object' && (0, _isEmpty3.default)(insertValues)) {
  44. return sql + this._emptyInsertValue;
  45. }
  46. var insertData = this._prepInsert(insertValues);
  47. if ((0, _isString3.default)(insertData)) {
  48. return sql + insertData;
  49. }
  50. if (insertData.columns.length === 0) {
  51. return '';
  52. }
  53. sql += '(' + this.formatter.columnize(insertData.columns) + ')';
  54. // backwards compatible error
  55. if (this.client.valueForUndefined !== null) {
  56. (0, _each3.default)(insertData.values, function (bindings) {
  57. (0, _each3.default)(bindings, function (binding) {
  58. 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).');
  59. });
  60. });
  61. }
  62. if (insertData.values.length === 1) {
  63. var parameters = this.formatter.parameterize(insertData.values[0], this.client.valueForUndefined);
  64. return sql + (' values (' + parameters + ')');
  65. }
  66. var blocks = [];
  67. var i = -1;
  68. while (++i < insertData.values.length) {
  69. var i2 = -1;
  70. var block = blocks[i] = [];
  71. var current = insertData.values[i];
  72. current = current === undefined ? this.client.valueForUndefined : current;
  73. while (++i2 < insertData.columns.length) {
  74. block.push(this.formatter.alias(this.formatter.parameter(current[i2]), this.formatter.wrap(insertData.columns[i2])));
  75. }
  76. blocks[i] = block.join(', ');
  77. }
  78. return sql + ' select ' + blocks.join(' union all select ');
  79. },
  80. // Compile a truncate table statement into SQL.
  81. truncate: function truncate() {
  82. var table = this.tableName;
  83. return {
  84. sql: 'delete from ' + table,
  85. output: function output() {
  86. return this.query({
  87. sql: 'delete from sqlite_sequence where name = ' + table
  88. }).catch(_noop3.default);
  89. }
  90. };
  91. },
  92. // Compiles a `columnInfo` query
  93. columnInfo: function columnInfo() {
  94. var column = this.single.columnInfo;
  95. return {
  96. sql: 'PRAGMA table_info(' + this.single.table + ')',
  97. output: function output(resp) {
  98. var maxLengthRegex = /.*\((\d+)\)/;
  99. var out = (0, _reduce3.default)(resp, function (columns, val) {
  100. var type = val.type;
  101. var maxLength = (maxLength = type.match(maxLengthRegex)) && maxLength[1];
  102. type = maxLength ? type.split('(')[0] : type;
  103. columns[val.name] = {
  104. type: type.toLowerCase(),
  105. maxLength: maxLength,
  106. nullable: !val.notnull,
  107. defaultValue: val.dflt_value
  108. };
  109. return columns;
  110. }, {});
  111. return column && out[column] || out;
  112. }
  113. };
  114. },
  115. limit: function limit() {
  116. var noLimit = !this.single.limit && this.single.limit !== 0;
  117. if (noLimit && !this.single.offset) return '';
  118. // Workaround for offset only,
  119. // see http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit
  120. return 'limit ' + this.formatter.parameter(noLimit ? -1 : this.single.limit);
  121. }
  122. });
  123. function emptyStr() {
  124. return '';
  125. }
  126. exports.default = QueryCompiler_SQLite3;
  127. module.exports = exports['default'];