mirror of
https://github.com/knex/knex.git
synced 2025-07-09 18:11:17 +00:00
139 lines
4.7 KiB
JavaScript
139 lines
4.7 KiB
JavaScript
'use strict';
|
|
|
|
// SQLite3: Column Builder & Compiler
|
|
// -------
|
|
module.exports = function(client) {
|
|
|
|
var _ = require('lodash');
|
|
var inherits = require('inherits');
|
|
var Schema = require('../../../schema');
|
|
|
|
// Table Builder
|
|
// -------
|
|
|
|
function TableBuilder_SQLite3() {
|
|
this.client = client;
|
|
Schema.TableBuilder.apply(this, arguments);
|
|
}
|
|
inherits(TableBuilder_SQLite3, Schema.TableBuilder);
|
|
|
|
// Table Compiler
|
|
// -------
|
|
|
|
function TableCompiler_SQLite3() {
|
|
this.client = client;
|
|
this.Formatter = client.Formatter;
|
|
this.SQLite3_DDL = client.SQLite3_DDL;
|
|
this.primaryKey = void 0;
|
|
Schema.TableCompiler.apply(this, arguments);
|
|
}
|
|
inherits(TableCompiler_SQLite3, Schema.TableCompiler);
|
|
|
|
// Create a new table.
|
|
TableCompiler_SQLite3.prototype.createQuery = function(columns, ifNot) {
|
|
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
|
var sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ');
|
|
|
|
// 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) {
|
|
for (var i = 0, l = columns.sql.length; i < l; i++) {
|
|
this.pushQuery({
|
|
sql: 'alter table ' + this.tableName() + ' add column ' + columns.sql[i],
|
|
bindings: columns.bindings[i]
|
|
});
|
|
}
|
|
};
|
|
|
|
// Compile a drop unique key command.
|
|
TableCompiler_SQLite3.prototype.dropUnique = function(columns, indexName) {
|
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
|
this.pushQuery('drop index ' + indexName);
|
|
};
|
|
|
|
TableCompiler_SQLite3.prototype.dropIndex = function(columns, indexName) {
|
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
|
this.pushQuery('drop index ' + indexName);
|
|
};
|
|
|
|
// Compile a unique key command.
|
|
TableCompiler_SQLite3.prototype.unique = function(columns, indexName) {
|
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
|
columns = this.formatter.columnize(columns);
|
|
this.pushQuery('create unique index ' + indexName + ' on ' + this.tableName() + ' (' + columns + ')');
|
|
};
|
|
|
|
// Compile a plain index key command.
|
|
TableCompiler_SQLite3.prototype.index = function(columns, indexName) {
|
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
|
columns = this.formatter.columnize(columns);
|
|
this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + ' (' + columns + ')');
|
|
};
|
|
|
|
TableCompiler_SQLite3.prototype.primary =
|
|
TableCompiler_SQLite3.prototype.foreign = function() {
|
|
if (this.method !== 'create') {
|
|
console.warn('SQLite3 Foreign & Primary keys may only be added on create');
|
|
}
|
|
};
|
|
|
|
TableCompiler_SQLite3.prototype.primaryKeys = function() {
|
|
var pks = _.where(this.grouped.alterTable || [], {method: 'primary'});
|
|
if (pks.length > 0 && pks[0].args.length > 0) {
|
|
var args = _.isArray(pks[0].args[0]) ? pks[0].args[0] : pks[0].args;
|
|
return ', primary key (' + this.formatter.columnize(args) + ')';
|
|
}
|
|
};
|
|
|
|
TableCompiler_SQLite3.prototype.foreignKeys = function() {
|
|
var sql = '';
|
|
var foreignKeys = _.where(this.grouped.alterTable || [], {method: 'foreign'});
|
|
for (var i = 0, l = foreignKeys.length; i < l; i++) {
|
|
var foreign = foreignKeys[i].args[0];
|
|
var column = this.formatter.columnize(foreign.column);
|
|
var references = this.formatter.columnize(foreign.references);
|
|
var foreignTable = this.formatter.wrap(foreign.inTable);
|
|
sql += ', foreign key(' + column + ') references ' + foreignTable + '(' + references + ')';
|
|
if (foreign.onDelete) sql += ' on delete ' + foreign.onDelete;
|
|
if (foreign.onUpdate) sql += ' on update ' + foreign.onUpdate;
|
|
}
|
|
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) {
|
|
var compiler = this;
|
|
this.pushQuery({
|
|
sql: 'PRAGMA table_info(' + this.tableName() + ')',
|
|
output: function(pragma) {
|
|
return new compiler.SQLite3_DDL(this, compiler, pragma).renameColumn(from, to);
|
|
}
|
|
});
|
|
};
|
|
|
|
TableCompiler_SQLite3.prototype.dropColumn = function(column) {
|
|
var compiler = this;
|
|
this.pushQuery({
|
|
sql: 'PRAGMA table_info(' + this.tableName() + ')',
|
|
output: function(pragma) {
|
|
return new compiler.SQLite3_DDL(this, compiler, pragma).dropColumn(column);
|
|
}
|
|
});
|
|
};
|
|
|
|
client.TableBuilder = TableBuilder_SQLite3;
|
|
client.TableCompiler = TableCompiler_SQLite3;
|
|
|
|
}; |