tablecompiler.js 5.5 KB

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