knex/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js

239 lines
7.8 KiB
JavaScript
Raw Normal View History

const { inherits } = require('util');
const TableCompiler = require('../../../schema/tablecompiler');
2016-03-02 17:07:05 +01:00
const filter = require('lodash/filter');
const values = require('lodash/values');
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.
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.createQuery = function (columns, ifNot) {
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);
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.addColumns = function (columns, prefix) {
if (prefix) {
throw new Error('Sqlite does not support alter column.');
}
for (let i = 0, l = columns.sql.length; i < l; i++) {
2016-03-02 17:07:05 +01:00
this.pushQuery({
sql: `alter table ${this.tableName()} add column ${columns.sql[i]}`,
bindings: columns.bindings[i],
2016-03-02 17:07:05 +01:00
});
}
};
// Compile a drop unique key command.
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.dropUnique = function (columns, indexName) {
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
};
// Compile a drop foreign key command.
TableCompiler_SQLite3.prototype.dropForeign = function (columns, indexName) {
const compiler = this;
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.dropForeign(columns, indexName);
},
});
};
// Compile a drop primary key command.
TableCompiler_SQLite3.prototype.dropPrimary = function (constraintName) {
const compiler = this;
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.dropPrimary(constraintName);
},
});
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.dropIndex = function (columns, indexName) {
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.
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.unique = function (columns, indexName) {
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);
this.pushQuery(
`create unique index ${indexName} on ${this.tableName()} (${columns})`
);
2016-03-02 17:07:05 +01:00
};
// Compile a plain index key command.
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.index = function (columns, indexName) {
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);
this.pushQuery(
`create index ${indexName} on ${this.tableName()} (${columns})`
);
2016-03-02 17:07:05 +01:00
};
/**
* Add a primary key to an existing table.
*
* @NOTE The `createQuery` method above handles table creation. Don't do anything regarding table
* creation in this method
*
* @param {string | string[]} columns - Column name(s) to assign as primary keys
* @param {string} [constraintName] - Custom name for the PK constraint
*/
TableCompiler_SQLite3.prototype.primary = function (columns, constraintName) {
const compiler = this;
columns = this.formatter.columnize(columns);
columns = Array.isArray(columns) ? columns : [columns];
if (this.method !== 'create' && this.method !== 'createIfNot') {
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.primary(columns, constraintName);
},
});
}
};
/**
* Add a foreign key constraint to an existing table
*
* @NOTE The `createQuery` method above handles foreign key constraints on table creation. Don't do
* anything regarding table creation in this method
*
* @param {object} foreignInfo - Information about the current column foreign setup
* @param {string | string[]} [foreignInfo.column] - Column in the current constraint
* @param {string | undefined} foreignInfo.keyName - Name of the foreign key constraint
* @param {string} foreignInfo.references - What column it references in the other table
* @param {string} foreignInfo.inTable - What table is referenced in this constraint
* @param {string} [foreignInfo.onUpdate] - What to do on updates
* @param {string} [foreignInfo.onDelete] - What to do on deletions
*/
TableCompiler_SQLite3.prototype.foreign = function (foreignInfo) {
const compiler = this;
2016-03-02 17:07:05 +01:00
if (this.method !== 'create' && this.method !== 'createIfNot') {
foreignInfo.column = this.formatter.columnize(foreignInfo.column);
foreignInfo.column = Array.isArray(foreignInfo.column)
? foreignInfo.column
: [foreignInfo.column];
foreignInfo.inTable = this.formatter.columnize(foreignInfo.inTable);
foreignInfo.references = this.formatter.columnize(foreignInfo.references);
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.foreign(foreignInfo);
},
});
2016-03-02 17:07:05 +01:00
}
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.primaryKeys = function () {
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) {
const columns = pks[0].args[0];
let constraintName = pks[0].args[1] || '';
if (constraintName) {
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
}
return `,${constraintName} primary key (${this.formatter.columnize(
columns
)})`;
2016-03-02 17:07:05 +01:00
}
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.foreignKeys = function () {
let sql = '';
const foreignKeys = filter(this.grouped.alterTable || [], {
method: 'foreign',
});
for (let i = 0, l = foreignKeys.length; i < l; i++) {
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);
let constraintName = foreign.keyName || '';
if (constraintName) {
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
}
sql += `,${constraintName} 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;
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.createTableBlock = function () {
return this.getColumns().concat().join(',');
2016-03-02 17:07:05 +01:00
};
// Compile a rename column command... very complex in sqlite
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.renameColumn = function (from, to) {
const compiler = this;
2016-03-02 17:07:05 +01:00
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.renameColumn(from, to);
},
2016-03-02 17:07:05 +01:00
});
};
2020-04-19 00:40:23 +02:00
TableCompiler_SQLite3.prototype.dropColumn = function () {
const compiler = this;
const columns = values(arguments);
2016-03-02 17:07:05 +01:00
this.pushQuery({
sql: `PRAGMA table_info(${this.tableName()})`,
output(pragma) {
return compiler.client
.ddl(compiler, pragma, this.connection)
.dropColumn(columns);
},
2016-03-02 17:07:05 +01:00
});
};
module.exports = TableCompiler_SQLite3;