2014-04-15 11:43:47 -04:00
|
|
|
// MySQL Table Builder & Compiler
|
|
|
|
// -------
|
|
|
|
module.exports = function(client) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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-04-16 01:23:50 -04:00
|
|
|
var hasComment = this.single.comment != void 0;
|
2014-04-16 03:22:47 -04:00
|
|
|
if (hasComment) {
|
|
|
|
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) {
|
2014-04-16 01:23:50 -04:00
|
|
|
var table = this.tableName();
|
|
|
|
var wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
|
|
|
|
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];
|
|
|
|
return this.query({
|
|
|
|
sql: 'alter table ' + table + ' change ' + wrapped + ' ' + column.Type
|
|
|
|
});
|
|
|
|
}
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
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;
|
|
|
|
|
|
|
|
};
|