2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import TableCompiler from '../../../schema/tablecompiler';
|
|
|
|
import * as helpers from '../../../helpers';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { filter } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// Table Compiler
|
|
|
|
// -------
|
|
|
|
|
|
|
|
function TableCompiler_SQLite3() {
|
|
|
|
TableCompiler.apply(this, arguments);
|
|
|
|
this.primaryKey = void 0;
|
|
|
|
}
|
|
|
|
inherits(TableCompiler_SQLite3, TableCompiler);
|
|
|
|
|
|
|
|
// Create a new table.
|
|
|
|
TableCompiler_SQLite3.prototype.createQuery = function(columns, ifNot) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
|
|
|
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// SQLite forces primary keys to be added when the table is initially created
|
|
|
|
// so we will need to check for a primary key commands and add the columns
|
|
|
|
// to the table's declaration here so they can be created on the tables.
|
|
|
|
sql += this.foreignKeys() || '';
|
|
|
|
sql += this.primaryKeys() || '';
|
|
|
|
sql += ')';
|
|
|
|
|
|
|
|
this.pushQuery(sql);
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.addColumns = function(columns) {
|
2016-05-17 01:01:34 +10:00
|
|
|
for (let i = 0, l = columns.sql.length; i < l; i++) {
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${this.tableName()} add column ${columns.sql[i]}`,
|
2016-03-02 17:07:05 +01:00
|
|
|
bindings: columns.bindings[i]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a drop unique key command.
|
|
|
|
TableCompiler_SQLite3.prototype.dropUnique = function(columns, indexName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.dropIndex = function(columns, indexName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a unique key command.
|
|
|
|
TableCompiler_SQLite3.prototype.unique = function(columns, indexName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-03-02 17:07:05 +01:00
|
|
|
columns = this.formatter.columnize(columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create unique index ${indexName} on ${this.tableName()} (${columns})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a plain index key command.
|
|
|
|
TableCompiler_SQLite3.prototype.index = function(columns, indexName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
indexName = indexName
|
|
|
|
? this.formatter.wrap(indexName)
|
|
|
|
: this._indexCommand('index', this.tableNameRaw, columns);
|
2016-03-02 17:07:05 +01:00
|
|
|
columns = this.formatter.columnize(columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create index ${indexName} on ${this.tableName()} (${columns})`);
|
2016-03-02 17:07:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.primary =
|
|
|
|
TableCompiler_SQLite3.prototype.foreign = function() {
|
|
|
|
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
2016-05-17 01:01:34 +10:00
|
|
|
helpers.warn('SQLite3 Foreign & Primary keys may only be added on create');
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.primaryKeys = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const pks = filter(this.grouped.alterTable || [], {method: 'primary'});
|
2016-03-02 17:07:05 +01:00
|
|
|
if (pks.length > 0 && pks[0].args.length > 0) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const args = Array.isArray(pks[0].args[0]) ? pks[0].args[0] : pks[0].args;
|
|
|
|
return `, primary key (${this.formatter.columnize(args)})`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.foreignKeys = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
let sql = '';
|
|
|
|
const foreignKeys = filter(this.grouped.alterTable || [], {method: 'foreign'});
|
|
|
|
for (let i = 0, l = foreignKeys.length; i < l; i++) {
|
2016-05-18 19:59:24 +10:00
|
|
|
const foreign = foreignKeys[i].args[0];
|
|
|
|
const column = this.formatter.columnize(foreign.column);
|
|
|
|
const references = this.formatter.columnize(foreign.references);
|
|
|
|
const foreignTable = this.formatter.wrap(foreign.inTable);
|
2016-05-17 01:01:34 +10:00
|
|
|
sql += `, foreign key(${column}) references ${foreignTable}(${references})`;
|
|
|
|
if (foreign.onDelete) sql += ` on delete ${foreign.onDelete}`;
|
|
|
|
if (foreign.onUpdate) sql += ` on update ${foreign.onUpdate}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
return sql;
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.createTableBlock = function() {
|
|
|
|
return this.getColumns().concat().join(',');
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a rename column command... very complex in sqlite
|
|
|
|
TableCompiler_SQLite3.prototype.renameColumn = function(from, to) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const compiler = this;
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return compiler.client.ddl(compiler, pragma, this.connection).renameColumn(from, to);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_SQLite3.prototype.dropColumn = function(column) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const compiler = this;
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
|
|
output(pragma) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return compiler.client.ddl(compiler, pragma, this.connection).dropColumn(column);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default TableCompiler_SQLite3;
|