2013-09-08 15:57:32 -04:00
|
|
|
// mysql
|
|
|
|
// -------
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// All of the "when.js" promise components needed in this module.
|
|
|
|
var when = require('when');
|
|
|
|
var nodefn = require('when/node/function');
|
|
|
|
var sequence = require('when/sequence');
|
|
|
|
|
|
|
|
// Other dependencies, including the `mysql` library,
|
|
|
|
// which needs to be added as a dependency to the project
|
|
|
|
// using this database.
|
|
|
|
var _ = require('underscore');
|
|
|
|
var mysql = require('mysql');
|
|
|
|
|
|
|
|
// All other local project modules needed in this scope.
|
|
|
|
var ServerBase = require('./base').ServerBase;
|
2013-09-11 23:36:55 -04:00
|
|
|
var baseGrammar = require('../base/grammar').BaseGrammar;
|
|
|
|
var baseSchemaGrammar = require('../base/schemagrammar').BaseSchemaGrammar;
|
2013-09-08 15:57:32 -04:00
|
|
|
var Helpers = require('../../lib/helpers').Helpers;
|
|
|
|
|
|
|
|
// Constructor for the MySQLClient.
|
|
|
|
exports.Client = ServerBase.extend({
|
|
|
|
|
|
|
|
dialect: 'mysql',
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
runQuery: function(connection, sql, bindings) {
|
|
|
|
return nodefn.call(connection.query.bind(connection), sql, bindings);
|
2013-09-08 15:57:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2013-09-11 23:36:55 -04:00
|
|
|
getRawConnection: function() {
|
2013-09-08 15:57:32 -04:00
|
|
|
var connection = mysql.createConnection(this.connectionSettings);
|
2013-09-11 23:36:55 -04:00
|
|
|
return nodefn.call(connection.connect.bind(connection)).yield(connection);
|
2013-05-04 12:23:55 -04:00
|
|
|
},
|
|
|
|
|
2013-09-04 17:32:32 -04:00
|
|
|
// Used to check if there is a conditional query needed to complete the next one.
|
|
|
|
checkSchema: function(builder) {
|
|
|
|
return function(conn) {
|
|
|
|
var sql = builder.sql;
|
|
|
|
if (sql.indexOf('alter table') === 0 && sql.indexOf('__datatype__') === (sql.length - 12)) {
|
|
|
|
var newSql = sql.replace('alter table', 'show fields from').split('change')[0] + ' where field = ?';
|
|
|
|
return nodefn.call(conn.query.bind(conn), newSql, [builder.commands[builder.currentIndex].from]).then(function(resp) {
|
|
|
|
var column = resp[0];
|
|
|
|
// Set to the datatype we're looking to change it to...
|
|
|
|
builder.sql = builder.sql.replace('__datatype__', column[0].Type);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2013-09-08 15:57:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Extends the standard sql grammar.
|
2013-09-08 15:57:32 -04:00
|
|
|
var grammar = exports.grammar = _.defaults({
|
2013-03-12 12:58:33 -04:00
|
|
|
|
|
|
|
// The keyword identifier wrapper format.
|
2013-03-12 11:52:08 -04:00
|
|
|
wrapValue: function(value) {
|
2013-09-08 15:57:32 -04:00
|
|
|
return (value !== '*' ? Helpers.format('`%s`', value) : "*");
|
2013-09-11 23:36:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Parses the response, according to the way mySQL works...
|
|
|
|
handleResponse: function(builder, response) {
|
|
|
|
if (builder.type === 'select') response = Helpers.skim(response[0]);
|
|
|
|
if (builder.type === 'insert') response = [response[0].insertId];
|
|
|
|
if (builder.type === 'delete' || builder.type === 'update') response = response.affectedRows;
|
|
|
|
return response;
|
2013-03-12 11:52:08 -04:00
|
|
|
}
|
2013-03-12 12:58:33 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
}, baseGrammar);
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Grammar for the schema builder.
|
2013-09-08 15:57:32 -04:00
|
|
|
var schemaGrammar = exports.schemaGrammar = _.defaults({
|
2013-03-12 12:45:54 -04:00
|
|
|
|
|
|
|
// The possible column modifiers.
|
2013-06-16 20:27:24 +10:00
|
|
|
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment'],
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
// Handle response for the schema.
|
|
|
|
handleResponse: function(builder, resp) {
|
|
|
|
if (builder.type === 'tableExists') return resp.length > 0;
|
|
|
|
if (builder.type === 'columnExists') return resp.length > 0;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2013-05-15 19:05:55 -04:00
|
|
|
// Compile a create table command.
|
|
|
|
compileCreateTable: function(blueprint, command) {
|
2013-09-11 23:36:55 -04:00
|
|
|
var sql = baseSchemaGrammar.compileCreateTable.call(this, blueprint, command);
|
2013-05-15 19:05:55 -04:00
|
|
|
var conn = blueprint.client.connectionSettings;
|
|
|
|
|
|
|
|
if (conn.charset) sql += ' default character set ' + conn.charset;
|
|
|
|
if (conn.collation) sql += ' collate ' + conn.collation;
|
2013-05-15 19:25:51 -04:00
|
|
|
if (blueprint.isEngine) {
|
|
|
|
sql += ' engine = ' + blueprint.isEngine;
|
2013-05-15 19:05:55 -04:00
|
|
|
}
|
|
|
|
|
2013-07-17 06:16:41 -04:00
|
|
|
// Checks if the table is commented
|
|
|
|
var isCommented = this.getCommandByName(blueprint, 'comment');
|
|
|
|
|
|
|
|
// TODO: Handle max comment length.
|
|
|
|
var maxTableCommentLength = 60;
|
|
|
|
if (isCommented) {
|
|
|
|
sql += " comment = '" + isCommented.comment + "'";
|
2013-06-16 20:27:24 +10:00
|
|
|
}
|
|
|
|
|
2013-05-15 19:05:55 -04:00
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile the query to determine if a table exists.
|
2013-05-04 02:57:12 -04:00
|
|
|
compileTableExists: function(blueprint) {
|
|
|
|
blueprint.bindings.unshift(blueprint.client.connectionSettings.database);
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
|
|
|
|
},
|
|
|
|
|
2013-09-04 19:13:02 -04:00
|
|
|
// Compile a query to determine if a column exists.
|
|
|
|
compileColumnExists: function(blueprint) {
|
|
|
|
return 'show columns from ' + this.wrapTable(blueprint) + ' like ?';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile an add command.
|
2013-05-22 13:10:42 -04:00
|
|
|
compileAdd: function(blueprint) {
|
2013-03-12 12:45:54 -04:00
|
|
|
var columns = this.prefixArray('add', this.getColumns(blueprint));
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' ' + columns.join(', ');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a primary key command.
|
|
|
|
compilePrimary: function(blueprint, command) {
|
2013-05-03 23:16:15 -04:00
|
|
|
command.name = null;
|
2013-03-12 12:45:54 -04:00
|
|
|
return this.compileKey(blueprint, command, 'primary key');
|
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile a unique key command.
|
|
|
|
compileUnique: function(blueprint, command) {
|
|
|
|
return this.compileKey(blueprint, command, 'unique');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a plain index key command.
|
|
|
|
compileIndex: function(blueprint, command) {
|
|
|
|
return this.compileKey(blueprint, command, 'index');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile an index creation command.
|
|
|
|
compileKey: function(blueprint, command, type) {
|
|
|
|
var columns = this.columnize(command.columns);
|
|
|
|
var table = this.wrapTable(blueprint);
|
|
|
|
return 'alter table ' + table + " add " + type + " " + command.index + "(" + columns + ")";
|
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile a drop column command.
|
|
|
|
compileDropColumn: function(blueprint, command) {
|
|
|
|
var columns = this.prefixArray('drop', this.wrapArray(command.columns));
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' ' + columns.join(', ');
|
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile a drop primary key command.
|
2013-05-22 13:10:42 -04:00
|
|
|
compileDropPrimary: function(blueprint) {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' drop primary key';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop unique key command.
|
|
|
|
compileDropUnique: function(blueprint, command) {
|
|
|
|
return this.compileDropIndex(blueprint, command);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop index command.
|
|
|
|
compileDropIndex: function(blueprint, command) {
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' drop index ' + command.index;
|
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Compile a drop foreign key command.
|
|
|
|
compileDropForeign: function(blueprint, command) {
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + " drop foreign key " + command.index;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a rename table command.
|
2013-05-02 00:21:49 -04:00
|
|
|
compileRenameTable: function(blueprint, command) {
|
2013-04-21 18:26:22 -04:00
|
|
|
return 'rename table ' + this.wrapTable(blueprint) + ' to ' + this.wrapTable(command.to);
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-09-04 17:32:32 -04:00
|
|
|
// Compile a rename column command.
|
|
|
|
compileRenameColumn: function(blueprint, command) {
|
|
|
|
return 'alter table ' + this.wrapTable(blueprint) + ' change ' +
|
|
|
|
this.wrapTable(command.from) + ' ' + this.wrapTable(command.to) + ' __datatype__';
|
|
|
|
},
|
|
|
|
|
2013-07-17 06:16:41 -04:00
|
|
|
// Compiles the comment on the table.
|
|
|
|
compileComment: function(blueprint, command) {
|
|
|
|
// Handled on create table...
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a text type.
|
|
|
|
typeText: function(column) {
|
2013-04-21 18:26:22 -04:00
|
|
|
switch (column.length) {
|
|
|
|
case 'medium':
|
|
|
|
case 'mediumtext':
|
|
|
|
return 'mediumtext';
|
|
|
|
case 'long':
|
|
|
|
case 'longtext':
|
|
|
|
return 'longtext';
|
|
|
|
default:
|
|
|
|
return 'text';
|
|
|
|
}
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
2013-09-04 20:02:28 -04:00
|
|
|
// Create the column type definition for a bigint type.
|
|
|
|
typeBigInteger: function() {
|
|
|
|
return 'bigint';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a integer type.
|
|
|
|
typeInteger: function(column) {
|
2013-04-21 18:26:22 -04:00
|
|
|
return 'int(' + column.length + ')';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a float type.
|
|
|
|
typeFloat: function(column) {
|
|
|
|
return 'float(' + column.total + ',' + column.places + ')';
|
|
|
|
},
|
2013-05-15 19:05:55 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Create the column definition for a decimal type.
|
|
|
|
typeDecimal: function(column) {
|
|
|
|
return 'decimal(' + column.precision + ', ' + column.scale + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a boolean type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeBoolean: function() {
|
2013-04-16 12:37:50 -04:00
|
|
|
return 'tinyint(1)';
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a enum type.
|
|
|
|
typeEnum: function(column) {
|
|
|
|
return "enum('" + column.allowed.join("', '") + "')";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a date-time type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeDateTime: function() {
|
2013-03-12 12:45:54 -04:00
|
|
|
return 'datetime';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a timestamp type.
|
2013-05-22 13:10:42 -04:00
|
|
|
typeTimestamp: function() {
|
2013-07-17 08:38:33 -04:00
|
|
|
return 'timestamp';
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
2013-04-21 18:26:22 -04:00
|
|
|
// Create the column definition for a bit type.
|
|
|
|
typeBit: function(column) {
|
|
|
|
return column.length !== false ? 'bit(' + column.length + ')' : 'bit';
|
|
|
|
},
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Get the SQL for an unsigned column modifier.
|
|
|
|
modifyUnsigned: function(blueprint, column) {
|
2013-05-03 12:51:54 -04:00
|
|
|
if (column.isUnsigned) return ' unsigned';
|
2013-03-12 12:45:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for a default column modifier.
|
|
|
|
modifyDefault: function(blueprint, column) {
|
2013-05-03 10:03:44 -04:00
|
|
|
// TODO - no default on blob/text
|
2013-05-22 12:37:29 -04:00
|
|
|
if (column.defaultValue != void 0 && column.type != 'blob' && column.type.indexOf('text') === -1) {
|
2013-03-12 12:45:54 -04:00
|
|
|
return " default '" + this.getDefaultValue(column.defaultValue) + "'";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for an auto-increment column modifier.
|
|
|
|
modifyIncrement: function(blueprint, column) {
|
2013-09-11 23:36:55 -04:00
|
|
|
if (column.autoIncrement && (column.type == 'integer' || column.type == 'bigInteger')) {
|
2013-09-04 18:34:59 -04:00
|
|
|
return ' not null auto_increment primary key';
|
2013-03-12 12:45:54 -04:00
|
|
|
}
|
2013-05-03 12:51:54 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Get the SQL for an "after" column modifier.
|
|
|
|
modifyAfter: function(blueprint, column) {
|
|
|
|
if (column.isAfter) {
|
|
|
|
return ' after ' + this.wrap(column.isAfter);
|
|
|
|
}
|
2013-06-16 20:27:24 +10:00
|
|
|
},
|
|
|
|
|
2013-07-17 06:16:41 -04:00
|
|
|
// Get the SQL for a comment column modifier.
|
2013-06-16 20:27:24 +10:00
|
|
|
modifyComment: function(blueprint, column) {
|
2013-07-17 06:16:41 -04:00
|
|
|
// TODO: Look into limiting this length.
|
2013-06-16 20:27:24 +10:00
|
|
|
var maxColumnCommentLength = 255;
|
2013-07-17 06:16:41 -04:00
|
|
|
if (column.isCommented && _.isString(column.isCommented)) {
|
|
|
|
return " comment '" + column.isCommented + "'";
|
2013-06-16 20:27:24 +10:00
|
|
|
}
|
2013-03-12 12:45:54 -04:00
|
|
|
}
|
2013-05-03 12:51:54 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
}, baseSchemaGrammar, grammar);
|