2013-04-30 18:29:53 -04:00
|
|
|
var Q = require('q');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var util = require('util');
|
2013-05-02 00:21:49 -04:00
|
|
|
var base = require('./base');
|
|
|
|
var mysql = require('mysql');
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
// Constructor for the MysqlClient
|
2013-04-30 18:29:53 -04:00
|
|
|
var MysqlClient = module.exports = function(name, options) {
|
2013-05-02 00:21:49 -04:00
|
|
|
base.setup.call(this, MysqlClient, name, options);
|
2013-04-30 18:29:53 -04:00
|
|
|
};
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
_.extend(MysqlClient.prototype, base.protoProps, {
|
|
|
|
|
2013-05-04 12:23:55 -04:00
|
|
|
// Execute a query on the specified Builder or QueryBuilder
|
|
|
|
// interface. If a `connection` is specified, use it, otherwise
|
|
|
|
// acquire a connection, and then dispose of it when we're done.
|
|
|
|
query: function(builder) {
|
|
|
|
var emptyConnection = !builder._connection;
|
2013-05-04 14:36:41 -04:00
|
|
|
var debug = this.debug || builder._debug;
|
2013-05-04 12:23:55 -04:00
|
|
|
var instance = this;
|
|
|
|
return Q((builder._connection || this.getConnection()))
|
|
|
|
.then(function(conn) {
|
|
|
|
var dfd = Q.defer();
|
|
|
|
|
|
|
|
// If we have a debug flag set, console.log the query.
|
2013-05-04 14:36:41 -04:00
|
|
|
if (debug) base.debug(builder, conn);
|
2013-05-04 12:23:55 -04:00
|
|
|
|
|
|
|
// Call the querystring and then release the client
|
|
|
|
conn.query(builder.sql, builder.bindings, function (err, resp) {
|
2013-05-04 14:36:41 -04:00
|
|
|
if (err) { return dfd.reject(err); }
|
2013-05-07 13:31:42 -04:00
|
|
|
|
2013-05-08 12:27:33 -04:00
|
|
|
if (builder._source === 'Raw') return dfd.resolve(resp);
|
|
|
|
|
2013-05-04 14:36:41 -04:00
|
|
|
if (builder._source === 'SchemaBuilder') {
|
|
|
|
if (builder.type === 'tableExists') {
|
|
|
|
if (resp.length > 0) return dfd.resolve(_.pick(resp, _.keys(resp)));
|
|
|
|
return dfd.reject(new Error('Table does not exist:' + builder.sql));
|
|
|
|
} else {
|
|
|
|
return dfd.resolve(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (builder.type === 'select') {
|
|
|
|
resp = base.skim(resp);
|
2013-05-04 20:06:39 -04:00
|
|
|
} else if (builder.type === 'insert') {
|
2013-05-04 14:36:41 -04:00
|
|
|
resp = [resp.insertId];
|
2013-05-04 20:06:39 -04:00
|
|
|
} else if (builder.type === 'delete' || builder.type === 'update') {
|
2013-05-04 14:36:41 -04:00
|
|
|
resp = resp.affectedRows;
|
2013-05-04 20:06:39 -04:00
|
|
|
} else {
|
|
|
|
resp = '';
|
2013-05-04 14:36:41 -04:00
|
|
|
}
|
|
|
|
|
2013-05-04 12:23:55 -04:00
|
|
|
dfd.resolve(resp);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Empty the connection after we run the query, unless one was specifically
|
|
|
|
// set (in the case of transactions, etc).
|
|
|
|
return dfd.promise.fin(function() {
|
|
|
|
if (emptyConnection) instance.pool.release(conn);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
getRawConnection: function() {
|
|
|
|
var conn = mysql.createConnection(this.connectionSettings);
|
|
|
|
conn.connect();
|
|
|
|
return conn;
|
2013-04-30 18:29:53 -04:00
|
|
|
}
|
2013-05-03 12:51:54 -04:00
|
|
|
|
2013-04-30 18:29:53 -04:00
|
|
|
});
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Extends the standard sql grammar.
|
2013-04-30 18:29:53 -04:00
|
|
|
MysqlClient.grammar = {
|
2013-03-12 12:58:33 -04:00
|
|
|
|
|
|
|
// The keyword identifier wrapper format.
|
2013-03-12 11:52:08 -04:00
|
|
|
wrapValue: function(value) {
|
|
|
|
return (value !== '*' ? util.format('`%s`', value) : "*");
|
|
|
|
}
|
2013-03-12 12:58:33 -04:00
|
|
|
|
2013-03-12 11:52:08 -04:00
|
|
|
};
|
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
// Grammar for the schema builder.
|
2013-05-03 10:03:44 -04:00
|
|
|
MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar, {
|
2013-03-12 12:45:54 -04:00
|
|
|
|
|
|
|
// The possible column modifiers.
|
2013-04-21 18:26:22 -04:00
|
|
|
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'],
|
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 = ?';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile an add command.
|
|
|
|
compileAdd: function(blueprint, command) {
|
|
|
|
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');
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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 + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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(', ');
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compile a drop primary key command.
|
|
|
|
compileDropPrimary: function(blueprint, command) {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a string type.
|
|
|
|
typeString: function(column) {
|
|
|
|
return "varchar(" + column.length + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a integer type.
|
|
|
|
typeInteger: function(column) {
|
2013-04-21 18:26:22 -04:00
|
|
|
return 'int(' + column.length + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a tiny integer type.
|
|
|
|
typeTinyInteger: function() {
|
|
|
|
return 'tinyint';
|
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 + ')';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
typeBoolean: function(column) {
|
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 type.
|
|
|
|
typeDate: function(column) {
|
|
|
|
return 'date';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a date-time type.
|
|
|
|
typeDateTime: function(column) {
|
|
|
|
return 'datetime';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a time type.
|
|
|
|
typeTime: function(column) {
|
|
|
|
return 'time';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create the column definition for a timestamp type.
|
|
|
|
typeTimestamp: function(column) {
|
|
|
|
return 'timestamp default 0';
|
|
|
|
},
|
|
|
|
|
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
|
|
|
// Create the column definition for a binary type.
|
|
|
|
typeBinary: function(column) {
|
|
|
|
return 'blob';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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 nullable column modifier.
|
|
|
|
modifyNullable: function(blueprint, column) {
|
|
|
|
return column.isNullable ? ' null' : ' not null';
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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-04 02:57:12 -04:00
|
|
|
if (column.defaultValue && 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) {
|
|
|
|
if (column.type == 'integer' && column.autoIncrement) {
|
|
|
|
return ' auto_increment primary key';
|
|
|
|
}
|
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-03-12 12:45:54 -04:00
|
|
|
}
|
2013-05-03 12:51:54 -04:00
|
|
|
|
2013-03-12 12:45:54 -04:00
|
|
|
});
|