2013-09-13 13:50:41 -04:00
|
|
|
// MySQL
|
2013-09-08 15:57:32 -04:00
|
|
|
// -------
|
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');
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
var Helpers = require('../../lib/helpers').Helpers;
|
|
|
|
|
2013-09-17 06:54:26 -04:00
|
|
|
var grammar = require('./mysql/grammar').grammar;
|
|
|
|
var schemaGrammar = require('./mysql/schemagrammar').schemaGrammar;
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Constructor for the MySQLClient.
|
|
|
|
exports.Client = ServerBase.extend({
|
|
|
|
|
|
|
|
dialect: 'mysql',
|
|
|
|
|
2013-09-17 06:54:26 -04:00
|
|
|
// Attach the appropriate grammar definitions onto the current client.
|
|
|
|
attachGrammars: function() {
|
|
|
|
this.grammar = grammar;
|
|
|
|
this.schemaGrammar = schemaGrammar;
|
|
|
|
},
|
|
|
|
|
2013-09-13 17:20:40 -04:00
|
|
|
// Runs the query on the specified connection, providing the bindings
|
|
|
|
// and any other necessary prep work.
|
2013-09-12 01:00:44 -04:00
|
|
|
runQuery: function(connection, sql, bindings, builder) {
|
2013-09-13 13:27:00 -04:00
|
|
|
if (!connection) throw new Error('No database connection exists for the query');
|
2013-09-13 11:40:51 -04:00
|
|
|
if (builder.flags.options) sql = _.extend({sql: sql}, builder.flags.options);
|
2013-09-12 01:00:44 -04:00
|
|
|
if (builder._source === 'SchemaBuilder') {
|
|
|
|
sql = this.advancedQuery(connection, sql, bindings, builder);
|
|
|
|
}
|
|
|
|
return when(sql).then(function(sql) {
|
|
|
|
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-13 16:50:51 -04:00
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
|
|
|
destroyRawConnection: function(connection) {
|
|
|
|
connection.end();
|
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.
|
2013-09-12 01:00:44 -04:00
|
|
|
advancedQuery: function(connection, sql, bindings, builder) {
|
|
|
|
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(connection.query.bind(connection), newSql, [builder.commands[builder.currentIndex].from]).then(function(resp) {
|
|
|
|
var column = resp[0];
|
|
|
|
// Set to the datatype we're looking to change it to...
|
|
|
|
return sql.replace('__datatype__', column[0].Type);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sql;
|
2013-04-30 18:29:53 -04:00
|
|
|
}
|
2013-05-03 12:51:54 -04:00
|
|
|
|
2013-09-17 06:54:26 -04:00
|
|
|
});
|