2014-09-01 16:56:30 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// MySQL Table Builder & Compiler
|
|
|
|
// -------
|
|
|
|
module.exports = function(client) {
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
var Schema = require('../../../schema');
|
2014-09-01 16:56:30 +02:00
|
|
|
var helpers = require('../../../helpers');
|
2014-04-15 11:43:47 -04:00
|
|
|
|
2015-02-04 11:03:24 -05:00
|
|
|
var Promise = require('../../../promise');
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
// 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);
|
|
|
|
|
2014-08-14 15:32:26 -04:00
|
|
|
TableCompiler_MySQL.prototype.createQuery = function(columns, ifNot) {
|
|
|
|
var createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
|
|
|
var conn = {}, sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
// 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;
|
2014-08-14 15:32:26 -04:00
|
|
|
if (charset) sql += ' default character set ' + charset;
|
2014-04-15 11:43:47 -04:00
|
|
|
if (collation) sql += ' collate ' + collation;
|
2014-08-14 15:32:26 -04:00
|
|
|
if (engine) sql += ' engine = ' + engine;
|
2014-04-15 11:43:47 -04:00
|
|
|
|
2014-09-03 11:04:10 +02:00
|
|
|
if (this.single.comment) {
|
2014-04-16 03:22:47 -04:00
|
|
|
var comment = (this.single.comment || '');
|
2014-04-16 04:29:20 -04:00
|
|
|
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
|
2014-04-16 03:22:47 -04:00
|
|
|
sql += " comment = '" + comment + "'";
|
|
|
|
}
|
2014-04-15 11:43:47 -04:00
|
|
|
|
|
|
|
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) {
|
2015-02-04 11:03:24 -05:00
|
|
|
var compiler = this;
|
|
|
|
var table = this.tableName();
|
|
|
|
var wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
|
2014-04-16 01:23:50 -04:00
|
|
|
this.pushQuery({
|
2014-04-15 11:43:47 -04:00
|
|
|
sql: 'show fields from ' + table + ' where field = ' +
|
|
|
|
this.formatter.parameter(from),
|
|
|
|
output: function(resp) {
|
|
|
|
var column = resp[0];
|
2015-02-04 11:03:24 -05:00
|
|
|
var runner = this;
|
|
|
|
|
|
|
|
return compiler.getFKRefs(runner).get(0)
|
|
|
|
.then(function (refs) {
|
|
|
|
return Promise.try(function () {
|
|
|
|
if (!refs.length) { return; }
|
|
|
|
return compiler.dropFKRefs(runner, refs);
|
|
|
|
}).then(function () {
|
|
|
|
return runner.query({
|
|
|
|
sql: 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
if (!refs.length) { return; }
|
|
|
|
return compiler.createFKRefs(runner, refs.map(function (ref) {
|
|
|
|
if (ref.REFERENCED_COLUMN_NAME === from) {
|
|
|
|
ref.REFERENCED_COLUMN_NAME = to;
|
|
|
|
}
|
|
|
|
if (ref.COLUMN_NAME === from) {
|
|
|
|
ref.COLUMN_NAME = to;
|
|
|
|
}
|
|
|
|
return ref;
|
|
|
|
}));
|
|
|
|
});
|
2014-04-15 11:43:47 -04:00
|
|
|
});
|
|
|
|
}
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
2015-02-04 11:03:24 -05:00
|
|
|
TableCompiler_MySQL.prototype.getFKRefs = function (runner) {
|
|
|
|
var formatter = new this.Formatter();
|
|
|
|
|
|
|
|
var sql = 'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, '+
|
|
|
|
' KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, '+
|
|
|
|
' RC.UPDATE_RULE, RC.DELETE_RULE '+
|
|
|
|
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU '+
|
|
|
|
'JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC '+
|
|
|
|
' USING(CONSTRAINT_NAME)' +
|
|
|
|
'WHERE KCU.REFERENCED_TABLE_NAME = ' + formatter.parameter(this.tableNameRaw) + ' '+
|
|
|
|
' AND KCU.CONSTRAINT_SCHEMA = ' + formatter.parameter(this.client.databaseName);
|
|
|
|
|
|
|
|
return runner.query({
|
|
|
|
sql: sql,
|
|
|
|
bindings: formatter.bindings
|
|
|
|
});
|
|
|
|
};
|
|
|
|
TableCompiler_MySQL.prototype.dropFKRefs = function (runner, refs) {
|
|
|
|
var formatter = new this.Formatter();
|
|
|
|
|
|
|
|
return Promise.all(refs.map(function (ref) {
|
|
|
|
var constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
|
|
return runner.query({
|
|
|
|
sql: 'alter table ' + this.tableName() + ' drop foreign key ' + constraintName
|
|
|
|
});
|
|
|
|
}.bind(this)));
|
|
|
|
};
|
|
|
|
TableCompiler_MySQL.prototype.createFKRefs = function (runner, refs) {
|
|
|
|
var formatter = new this.Formatter();
|
|
|
|
|
|
|
|
return Promise.all(refs.map(function (ref) {
|
|
|
|
var keyName = formatter.wrap(ref.COLUMN_NAME);
|
|
|
|
var column = formatter.columnize(ref.COLUMN_NAME);
|
|
|
|
var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
|
|
|
|
var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
|
|
|
var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
|
|
|
|
var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
|
|
|
|
|
|
|
|
return runner.query({
|
|
|
|
sql: 'alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' +
|
|
|
|
'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete
|
|
|
|
});
|
|
|
|
}.bind(this)));
|
|
|
|
};
|
2014-04-15 11:43:47 -04:00
|
|
|
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.
|
2014-06-04 11:29:21 -04:00
|
|
|
TableCompiler_MySQL.prototype.dropIndex = function(columns, indexName) {
|
|
|
|
indexName = indexName || this._indexCommand('index', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compile a drop foreign key command.
|
2014-06-04 11:29:21 -04:00
|
|
|
TableCompiler_MySQL.prototype.dropForeign = function(columns, indexName) {
|
|
|
|
indexName = indexName || this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' drop foreign key ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2014-06-04 11:29:21 -04:00
|
|
|
TableCompiler_MySQL.prototype.dropUnique = function(column, indexName) {
|
|
|
|
indexName = indexName || this._indexCommand('unique', this.tableNameRaw, column);
|
|
|
|
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
|
2014-04-15 11:43:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
client.TableBuilder = TableBuilder_MySQL;
|
|
|
|
client.TableCompiler = TableCompiler_MySQL;
|
|
|
|
|
2014-09-01 17:18:45 +02:00
|
|
|
};
|