mirror of
https://github.com/knex/knex.git
synced 2025-07-12 19:40:33 +00:00
142 lines
5.0 KiB
JavaScript
142 lines
5.0 KiB
JavaScript
![]() |
// MySQL Table Builder & Compiler
|
||
|
// -------
|
||
|
module.exports = function(client) {
|
||
|
|
||
|
var _ = require('lodash');
|
||
|
var inherits = require('inherits');
|
||
|
var Schema = require('../../../schema');
|
||
|
|
||
|
// Table Builder
|
||
|
// ------
|
||
|
|
||
|
function TableBuilder_MySQL() {
|
||
|
this.client = client;
|
||
|
Schema.TableBuilder.apply(this, arguments);
|
||
|
}
|
||
|
inherits(TableBuilder_MySQL, Schema.TableBuilder);
|
||
|
|
||
|
// Table Compiler
|
||
|
// ------
|
||
|
|
||
|
function TableCompiler_MySQL() {
|
||
|
this.client = client;
|
||
|
this.Formatter = client.Formatter;
|
||
|
Schema.TableCompiler.apply(this, arguments);
|
||
|
}
|
||
|
inherits(TableCompiler_MySQL, Schema.TableCompiler);
|
||
|
|
||
|
TableCompiler_MySQL.prototype.createQuery = function(columns) {
|
||
|
var conn = {}, sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
||
|
|
||
|
// Check if the connection settings are set.
|
||
|
if (client.connectionSettings) {
|
||
|
conn = client.connectionSettings;
|
||
|
}
|
||
|
|
||
|
var charset = this.single.charset || conn.charset || '';
|
||
|
var collation = this.single.collate || conn.collate || '';
|
||
|
var engine = this.single.engine || '';
|
||
|
|
||
|
// var conn = builder.client.connectionSettings;
|
||
|
if (charset) sql += ' default character set ' + charset;
|
||
|
if (collation) sql += ' collate ' + collation;
|
||
|
if (engine) sql += ' engine = ' + engine;
|
||
|
|
||
|
// // TODO: Handle max comment length ?
|
||
|
var maxTableCommentLength = 60;
|
||
|
var hasComment = _.has(this._single, 'comment');
|
||
|
if (hasComment) sql += " comment = '" + (hasComment || '') + "'";
|
||
|
|
||
|
this.pushQuery(sql);
|
||
|
};
|
||
|
|
||
|
TableCompiler_MySQL.prototype.addColumnsPrefix = 'add ';
|
||
|
TableCompiler_MySQL.prototype.dropColumnPrefix = 'drop ';
|
||
|
|
||
|
// Compiles the comment on the table.
|
||
|
TableCompiler_MySQL.prototype.comment = function(comment) {
|
||
|
this.pushQuery('alter table ' + this.tableName() + " comment = '" + comment + "'");
|
||
|
};
|
||
|
|
||
|
TableCompiler_MySQL.prototype.changeType = function() {
|
||
|
// alter table + table + ' modify ' + wrapped + '// type';
|
||
|
};
|
||
|
|
||
|
// Renames a column on the table.
|
||
|
TableCompiler_MySQL.prototype.renameColumn = function(from, to) {
|
||
|
// var wrapped = this._wrap(from) + ' ' + this._wrap(to);
|
||
|
// var table = this.tableName();
|
||
|
return {
|
||
|
sql: 'show fields from ' + table + ' where field = ' +
|
||
|
this.formatter.parameter(from),
|
||
|
output: function(resp) {
|
||
|
var column = resp[0];
|
||
|
return this.query({
|
||
|
sql: 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
};
|
||
|
|
||
|
TableCompiler_MySQL.prototype.index = function(columns, indexName) {
|
||
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
||
|
this.pushQuery('alter table ' + this.tableName() + " add index " + indexName + "(" + this.formatter.columnize(columns) + ")");
|
||
|
};
|
||
|
|
||
|
TableCompiler_MySQL.prototype.primary = function(columns, indexName) {
|
||
|
indexName = indexName || this._indexCommand('primary', this.tableNameRaw, columns);
|
||
|
this.pushQuery('alter table ' + this.tableName() + " add primary key " + indexName + "(" + this.formatter.columnize(columns) + ")");
|
||
|
};
|
||
|
|
||
|
TableCompiler_MySQL.prototype.unique = function(columns, indexName) {
|
||
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, columns);
|
||
|
this.pushQuery('alter table ' + this.tableName() + " add unique " + indexName + "(" + this.formatter.columnize(columns) + ")");
|
||
|
};
|
||
|
|
||
|
// Compile a drop index command.
|
||
|
TableCompiler_MySQL.prototype.dropIndex = function(key) {
|
||
|
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + key);
|
||
|
};
|
||
|
|
||
|
// Compile a drop foreign key command.
|
||
|
TableCompiler_MySQL.prototype.dropForeign = function(key) {
|
||
|
this.pushQuery('alter table ' + this.tableName() + ' drop foreign key ' + key);
|
||
|
};
|
||
|
|
||
|
// Compile a drop primary key command.
|
||
|
TableCompiler_MySQL.prototype.dropPrimary = function() {
|
||
|
this.pushQuery('alter table ' + this.tableName() + ' drop primary key');
|
||
|
};
|
||
|
|
||
|
// Compile a drop unique key command.
|
||
|
TableCompiler_MySQL.prototype.dropUnique = function(key) {
|
||
|
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + key);
|
||
|
};
|
||
|
|
||
|
// Compile a foreign key command.
|
||
|
TableCompiler_MySQL.prototype.foreign = function(foreignData) {
|
||
|
var sql = '';
|
||
|
if (foreignData.inTable && foreignData.references) {
|
||
|
var keyName = this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
|
||
|
var column = this.formatter.columnize(foreignData.column);
|
||
|
var references = this.formatter.columnize(foreignData.references);
|
||
|
var inTable = this.formatter.wrap(foreignData.inTable);
|
||
|
|
||
|
sql = 'alter table ' + this.tableName() + ' add constraint ' + keyName + ' ';
|
||
|
sql += 'foreign key (' + column + ') references ' + inTable + ' (' + references + ')';
|
||
|
|
||
|
// Once we have the basic foreign key creation statement constructed we can
|
||
|
// build out the syntax for what should happen on an update or delete of
|
||
|
// the affected columns, which will get something like 'cascade', etc.
|
||
|
if (foreignData.onDelete) sql += ' on delete ' + foreignData.onDelete;
|
||
|
if (foreignData.onUpdate) sql += ' on update ' + foreignData.onUpdate;
|
||
|
}
|
||
|
return {
|
||
|
sql: sql
|
||
|
};
|
||
|
};
|
||
|
|
||
|
client.TableBuilder = TableBuilder_MySQL;
|
||
|
client.TableCompiler = TableCompiler_MySQL;
|
||
|
|
||
|
};
|