tablecompiler.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _filter2 = require('lodash/filter');
  4. var _filter3 = _interopRequireDefault(_filter2);
  5. var _inherits = require('inherits');
  6. var _inherits2 = _interopRequireDefault(_inherits);
  7. var _tablecompiler = require('../../../schema/tablecompiler');
  8. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  9. var _helpers = require('../../../helpers');
  10. var helpers = _interopRequireWildcard(_helpers);
  11. 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; } }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. // Table Compiler
  14. // -------
  15. function TableCompiler_SQLite3() {
  16. _tablecompiler2.default.apply(this, arguments);
  17. this.primaryKey = void 0;
  18. }
  19. (0, _inherits2.default)(TableCompiler_SQLite3, _tablecompiler2.default);
  20. // Create a new table.
  21. TableCompiler_SQLite3.prototype.createQuery = function (columns, ifNot) {
  22. var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
  23. var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ');
  24. // SQLite forces primary keys to be added when the table is initially created
  25. // so we will need to check for a primary key commands and add the columns
  26. // to the table's declaration here so they can be created on the tables.
  27. sql += this.foreignKeys() || '';
  28. sql += this.primaryKeys() || '';
  29. sql += ')';
  30. this.pushQuery(sql);
  31. };
  32. TableCompiler_SQLite3.prototype.addColumns = function (columns, prefix) {
  33. if (prefix) {
  34. throw new Error("Sqlite does not support alter column.");
  35. }
  36. for (var i = 0, l = columns.sql.length; i < l; i++) {
  37. this.pushQuery({
  38. sql: 'alter table ' + this.tableName() + ' add column ' + columns.sql[i],
  39. bindings: columns.bindings[i]
  40. });
  41. }
  42. };
  43. // Compile a drop unique key command.
  44. TableCompiler_SQLite3.prototype.dropUnique = function (columns, indexName) {
  45. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  46. this.pushQuery('drop index ' + indexName);
  47. };
  48. TableCompiler_SQLite3.prototype.dropIndex = function (columns, indexName) {
  49. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  50. this.pushQuery('drop index ' + indexName);
  51. };
  52. // Compile a unique key command.
  53. TableCompiler_SQLite3.prototype.unique = function (columns, indexName) {
  54. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  55. columns = this.formatter.columnize(columns);
  56. this.pushQuery('create unique index ' + indexName + ' on ' + this.tableName() + ' (' + columns + ')');
  57. };
  58. // Compile a plain index key command.
  59. TableCompiler_SQLite3.prototype.index = function (columns, indexName) {
  60. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  61. columns = this.formatter.columnize(columns);
  62. this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + ' (' + columns + ')');
  63. };
  64. TableCompiler_SQLite3.prototype.primary = TableCompiler_SQLite3.prototype.foreign = function () {
  65. if (this.method !== 'create' && this.method !== 'createIfNot') {
  66. helpers.warn('SQLite3 Foreign & Primary keys may only be added on create');
  67. }
  68. };
  69. TableCompiler_SQLite3.prototype.primaryKeys = function () {
  70. var pks = (0, _filter3.default)(this.grouped.alterTable || [], { method: 'primary' });
  71. if (pks.length > 0 && pks[0].args.length > 0) {
  72. var args = Array.isArray(pks[0].args[0]) ? pks[0].args[0] : pks[0].args;
  73. return ', primary key (' + this.formatter.columnize(args) + ')';
  74. }
  75. };
  76. TableCompiler_SQLite3.prototype.foreignKeys = function () {
  77. var sql = '';
  78. var foreignKeys = (0, _filter3.default)(this.grouped.alterTable || [], { method: 'foreign' });
  79. for (var i = 0, l = foreignKeys.length; i < l; i++) {
  80. var foreign = foreignKeys[i].args[0];
  81. var column = this.formatter.columnize(foreign.column);
  82. var references = this.formatter.columnize(foreign.references);
  83. var foreignTable = this.formatter.wrap(foreign.inTable);
  84. sql += ', foreign key(' + column + ') references ' + foreignTable + '(' + references + ')';
  85. if (foreign.onDelete) sql += ' on delete ' + foreign.onDelete;
  86. if (foreign.onUpdate) sql += ' on update ' + foreign.onUpdate;
  87. }
  88. return sql;
  89. };
  90. TableCompiler_SQLite3.prototype.createTableBlock = function () {
  91. return this.getColumns().concat().join(',');
  92. };
  93. // Compile a rename column command... very complex in sqlite
  94. TableCompiler_SQLite3.prototype.renameColumn = function (from, to) {
  95. var compiler = this;
  96. this.pushQuery({
  97. sql: 'PRAGMA table_info(' + this.tableName() + ')',
  98. output: function output(pragma) {
  99. return compiler.client.ddl(compiler, pragma, this.connection).renameColumn(from, to);
  100. }
  101. });
  102. };
  103. TableCompiler_SQLite3.prototype.dropColumn = function (column) {
  104. var compiler = this;
  105. this.pushQuery({
  106. sql: 'PRAGMA table_info(' + this.tableName() + ')',
  107. output: function output(pragma) {
  108. return compiler.client.ddl(compiler, pragma, this.connection).dropColumn(column);
  109. }
  110. });
  111. };
  112. exports.default = TableCompiler_SQLite3;
  113. module.exports = exports['default'];