2012-01-30 19:34:30 +00:00
|
|
|
var safeRequire = require('../utils').safeRequire;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2012-01-30 19:34:30 +00:00
|
|
|
var mysql = safeRequire('mysql');
|
2012-03-10 07:55:25 +00:00
|
|
|
var BaseSQL = require('../sql');
|
2011-10-08 17:11:26 +00:00
|
|
|
|
|
|
|
exports.initialize = function initializeSchema(schema, callback) {
|
2012-01-30 19:34:30 +00:00
|
|
|
if (!mysql) return;
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
var s = schema.settings;
|
|
|
|
schema.client = mysql.createClient({
|
|
|
|
host: s.host || 'localhost',
|
|
|
|
port: s.port || 3306,
|
2011-10-23 19:43:53 +00:00
|
|
|
user: s.username,
|
2011-10-08 17:11:26 +00:00
|
|
|
password: s.password,
|
|
|
|
database: s.database,
|
|
|
|
debug: s.debug
|
|
|
|
});
|
|
|
|
|
|
|
|
schema.adapter = new MySQL(schema.client);
|
2012-01-30 15:43:45 +00:00
|
|
|
// schema.client.query('SET TIME_ZONE = "+04:00"', callback);
|
|
|
|
process.nextTick(callback);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function MySQL(client) {
|
|
|
|
this._models = {};
|
|
|
|
this.client = client;
|
|
|
|
}
|
|
|
|
|
2012-03-10 07:55:25 +00:00
|
|
|
require('util').inherits(MySQL, BaseSQL);
|
2011-12-09 15:23:29 +00:00
|
|
|
|
2011-11-11 13:16:09 +00:00
|
|
|
MySQL.prototype.query = function (sql, callback) {
|
|
|
|
var time = Date.now();
|
|
|
|
var log = this.log;
|
2011-12-09 15:23:29 +00:00
|
|
|
if (typeof callback !== 'function') throw new Error('callback should be a function');
|
2011-11-11 13:16:09 +00:00
|
|
|
this.client.query(sql, function (err, data) {
|
2012-03-11 04:48:38 +00:00
|
|
|
if (log) log(sql, time);
|
2011-11-11 13:16:09 +00:00
|
|
|
callback(err, data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-10-23 19:43:53 +00:00
|
|
|
/**
|
|
|
|
* Must invoke callback(err, id)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
MySQL.prototype.create = function (model, data, callback) {
|
2011-10-23 19:43:53 +00:00
|
|
|
var fields = this.toFields(model, data);
|
2012-03-10 10:32:14 +00:00
|
|
|
var sql = 'INSERT INTO ' + this.tableEscaped(model);
|
2011-10-23 19:43:53 +00:00
|
|
|
if (fields) {
|
|
|
|
sql += ' SET ' + fields;
|
|
|
|
} else {
|
|
|
|
sql += ' VALUES ()';
|
|
|
|
}
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err, info) {
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err, info && info.insertId);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.toFields = function (model, data) {
|
|
|
|
var fields = [];
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
if (props[key]) {
|
2012-01-19 20:16:30 +00:00
|
|
|
fields.push('`' + key.replace(/\./g, '`.`') + '` = ' + this.toDatabase(props[key], data[key]));
|
2011-10-23 19:43:53 +00:00
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
2011-10-23 19:43:53 +00:00
|
|
|
return fields.join(',');
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2012-01-30 13:27:26 +00:00
|
|
|
function dateToMysql(val) {
|
|
|
|
return val.getUTCFullYear() + '-' +
|
|
|
|
fillZeros(val.getUTCMonth() + 1) + '-' +
|
|
|
|
fillZeros(val.getUTCDate()) + ' ' +
|
|
|
|
fillZeros(val.getUTCHours()) + ':' +
|
|
|
|
fillZeros(val.getUTCMinutes()) + ':' +
|
|
|
|
fillZeros(val.getUTCSeconds());
|
|
|
|
|
|
|
|
function fillZeros(v) {
|
|
|
|
return v < 10 ? '0' + v : v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 19:43:53 +00:00
|
|
|
MySQL.prototype.toDatabase = function (prop, val) {
|
|
|
|
if (val === null) return 'NULL';
|
2012-02-01 17:33:08 +00:00
|
|
|
if (val.constructor.name === 'Object') {
|
|
|
|
var operator = Object.keys(val)[0]
|
|
|
|
val = val[operator];
|
|
|
|
if (operator === 'between') {
|
|
|
|
return this.toDatabase(prop, val[0]) +
|
|
|
|
' AND ' +
|
|
|
|
this.toDatabase(prop, val[1]);
|
2012-02-04 11:25:07 +00:00
|
|
|
} else if (operator == 'inq' || operator == 'nin') {
|
2012-03-10 08:39:39 +00:00
|
|
|
if (!(val.propertyIsEnumerable('length')) && typeof val === 'object' && typeof val.length === 'number') { //if value is array
|
|
|
|
return val.join(',');
|
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
2012-02-01 17:33:08 +00:00
|
|
|
}
|
|
|
|
if (prop.type.name === 'Number') return val;
|
2011-10-23 19:43:53 +00:00
|
|
|
if (prop.type.name === 'Date') {
|
2011-11-12 12:04:07 +00:00
|
|
|
if (!val) return 'NULL';
|
2011-10-23 19:43:53 +00:00
|
|
|
if (!val.toUTCString) {
|
|
|
|
val = new Date(val);
|
|
|
|
}
|
2012-01-30 13:27:26 +00:00
|
|
|
return '"' + dateToMysql(val) + '"';
|
2011-10-23 19:43:53 +00:00
|
|
|
}
|
2011-11-19 17:16:37 +00:00
|
|
|
if (prop.type.name == "Boolean") return val ? 1 : 0;
|
2011-10-23 19:43:53 +00:00
|
|
|
return this.client.escape(val.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.fromDatabase = function (model, data) {
|
|
|
|
if (!data) return null;
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
var val = data[key];
|
|
|
|
if (props[key]) {
|
2012-03-01 15:02:20 +00:00
|
|
|
if (props[key].type.name === 'Date' && val !== null) {
|
2012-01-30 13:27:26 +00:00
|
|
|
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
|
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-10-23 19:43:53 +00:00
|
|
|
data[key] = val;
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2012-03-10 07:55:25 +00:00
|
|
|
MySQL.prototype.escapeName = function (name) {
|
2012-03-10 08:39:39 +00:00
|
|
|
return '`' + name.replace(/\./g, '`.`') + '`';
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.all = function all(model, filter, callback) {
|
2012-01-19 20:16:30 +00:00
|
|
|
|
2012-03-10 07:55:25 +00:00
|
|
|
var sql = 'SELECT * FROM ' + this.tableEscaped(model);
|
2012-01-19 20:16:30 +00:00
|
|
|
var self = this;
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
|
|
|
|
if (filter.where) {
|
|
|
|
sql += ' ' + buildWhere(filter.where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter.order) {
|
|
|
|
sql += ' ' + buildOrderBy(filter.order);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter.limit) {
|
|
|
|
sql += ' ' + buildLimit(filter.limit, filter.offset || 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.query(sql, function (err, data) {
|
2011-10-08 17:11:26 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err, []);
|
|
|
|
}
|
2012-01-30 13:27:26 +00:00
|
|
|
callback(null, data.map(function (obj) {
|
|
|
|
return self.fromDatabase(model, obj);
|
|
|
|
}));
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
2012-01-19 20:16:30 +00:00
|
|
|
return sql;
|
|
|
|
|
|
|
|
function buildWhere(conds) {
|
|
|
|
var cs = [];
|
|
|
|
Object.keys(conds).forEach(function (key) {
|
|
|
|
var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`'
|
2012-02-01 17:33:08 +00:00
|
|
|
var val = self.toDatabase(props[key], conds[key]);
|
2012-01-19 20:16:30 +00:00
|
|
|
if (conds[key] === null) {
|
|
|
|
cs.push(keyEscaped + ' IS NULL');
|
2012-02-01 17:33:08 +00:00
|
|
|
} else if (conds[key].constructor.name === 'Object') {
|
|
|
|
var condType = Object.keys(conds[key])[0];
|
|
|
|
var sqlCond = keyEscaped;
|
|
|
|
switch (condType) {
|
|
|
|
case 'gt':
|
2012-03-10 08:39:39 +00:00
|
|
|
sqlCond += ' > ';
|
|
|
|
break;
|
2012-02-01 17:33:08 +00:00
|
|
|
case 'gte':
|
2012-03-10 08:39:39 +00:00
|
|
|
sqlCond += ' >= ';
|
|
|
|
break;
|
2012-02-01 17:33:08 +00:00
|
|
|
case 'lt':
|
2012-03-10 08:39:39 +00:00
|
|
|
sqlCond += ' < ';
|
|
|
|
break;
|
2012-02-01 17:33:08 +00:00
|
|
|
case 'lte':
|
2012-03-10 08:39:39 +00:00
|
|
|
sqlCond += ' <= ';
|
|
|
|
break;
|
2012-02-01 17:33:08 +00:00
|
|
|
case 'between':
|
2012-03-10 08:39:39 +00:00
|
|
|
sqlCond += ' BETWEEN ';
|
|
|
|
break;
|
|
|
|
case 'inq':
|
|
|
|
sqlCond += ' IN ';
|
|
|
|
break;
|
|
|
|
case 'nin':
|
|
|
|
sqlCond += ' NOT IN ';
|
|
|
|
break;
|
|
|
|
case 'neq':
|
|
|
|
sqlCond + ' != ';
|
|
|
|
break;
|
2012-02-01 17:33:08 +00:00
|
|
|
}
|
2012-02-04 11:25:07 +00:00
|
|
|
sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val;
|
2012-02-01 17:33:08 +00:00
|
|
|
cs.push(sqlCond);
|
2012-01-19 20:16:30 +00:00
|
|
|
} else {
|
2012-02-01 17:33:08 +00:00
|
|
|
cs.push(keyEscaped + ' = ' + val);
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
});
|
2012-03-06 13:08:27 +00:00
|
|
|
if (cs.length === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
2012-01-19 20:16:30 +00:00
|
|
|
return 'WHERE ' + cs.join(' AND ');
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 20:16:30 +00:00
|
|
|
function buildOrderBy(order) {
|
|
|
|
if (typeof order === 'string') order = [order];
|
|
|
|
return 'ORDER BY ' + order.join(', ');
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2012-01-19 20:16:30 +00:00
|
|
|
|
|
|
|
function buildLimit(limit, offset) {
|
|
|
|
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
MySQL.prototype.autoupdate = function (cb) {
|
|
|
|
var self = this;
|
|
|
|
var wait = 0;
|
|
|
|
Object.keys(this._models).forEach(function (model) {
|
|
|
|
wait += 1;
|
2012-03-10 07:55:25 +00:00
|
|
|
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
|
2012-02-20 18:33:11 +00:00
|
|
|
if (!err && fields.length) {
|
|
|
|
self.alterTable(model, fields, done);
|
|
|
|
} else {
|
|
|
|
self.createTable(model, done);
|
|
|
|
}
|
2011-12-09 15:23:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
if (--wait === 0 && cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-20 18:33:11 +00:00
|
|
|
MySQL.prototype.isActual = function (cb) {
|
|
|
|
var ok = false;
|
|
|
|
var self = this;
|
|
|
|
var wait = 0;
|
|
|
|
Object.keys(this._models).forEach(function (model) {
|
|
|
|
wait += 1;
|
|
|
|
self.query('SHOW FIELDS FROM ' + model, function (err, fields) {
|
|
|
|
self.alterTable(model, fields, done, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err, needAlter) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
ok = ok || needAlter;
|
|
|
|
if (--wait === 0 && cb) {
|
|
|
|
cb(null, !ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) {
|
2011-12-09 15:23:29 +00:00
|
|
|
var self = this;
|
|
|
|
var m = this._models[model];
|
2012-02-20 18:33:11 +00:00
|
|
|
var propNames = Object.keys(m.properties).filter(function (name) {
|
|
|
|
return !!m.properties[name];
|
|
|
|
});
|
2011-12-09 15:23:29 +00:00
|
|
|
var sql = [];
|
2011-12-11 07:51:57 +00:00
|
|
|
|
|
|
|
// change/add new fields
|
|
|
|
propNames.forEach(function (propName) {
|
|
|
|
var found;
|
|
|
|
actualFields.forEach(function (f) {
|
|
|
|
if (f.Field === propName) {
|
|
|
|
found = f;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
actualize(propName, found);
|
|
|
|
} else {
|
|
|
|
sql.push('ADD COLUMN `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// drop columns
|
2011-12-09 15:23:29 +00:00
|
|
|
actualFields.forEach(function (f) {
|
2011-12-11 07:51:57 +00:00
|
|
|
var notFound = !~propNames.indexOf(f.Field);
|
|
|
|
if (f.Field === 'id') return;
|
|
|
|
if (notFound || !m.properties[f.Field]) {
|
|
|
|
sql.push('DROP COLUMN `' + f.Field + '`');
|
2011-12-09 15:23:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sql.length) {
|
2012-02-20 18:33:11 +00:00
|
|
|
if (checkOnly) {
|
|
|
|
done(null, true);
|
|
|
|
} else {
|
|
|
|
this.query('ALTER TABLE `' + model + '` ' + sql.join(',\n'), done);
|
|
|
|
}
|
2011-12-09 15:23:29 +00:00
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
function actualize(propName, oldSettings) {
|
|
|
|
var newSettings = m.properties[propName];
|
2011-12-11 07:51:57 +00:00
|
|
|
if (newSettings && changed(newSettings, oldSettings)) {
|
2011-12-09 15:23:29 +00:00
|
|
|
sql.push('CHANGE COLUMN `' + propName + '` `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function changed(newSettings, oldSettings) {
|
|
|
|
if (oldSettings.Null === 'YES' && (newSettings.allowNull === false || newSettings.null === false)) return true;
|
|
|
|
if (oldSettings.Null === 'NO' && !(newSettings.allowNull === false || newSettings.null === false)) return true;
|
|
|
|
if (oldSettings.Type.toUpperCase() !== datatype(newSettings)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.propertiesSQL = function (model) {
|
|
|
|
var self = this;
|
|
|
|
var sql = ['`id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY'];
|
|
|
|
Object.keys(this._models[model].properties).forEach(function (prop) {
|
|
|
|
sql.push('`' + prop + '` ' + self.propertySettingsSQL(model, prop));
|
|
|
|
});
|
|
|
|
return sql.join(',\n ');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.propertySettingsSQL = function (model, prop) {
|
|
|
|
var p = this._models[model].properties[prop];
|
|
|
|
return datatype(p) + ' ' +
|
2011-12-11 07:51:57 +00:00
|
|
|
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
|
2011-12-09 15:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function datatype(p) {
|
2012-02-20 18:44:02 +00:00
|
|
|
var dt = '';
|
2011-12-09 15:23:29 +00:00
|
|
|
switch (p.type.name) {
|
|
|
|
case 'String':
|
2012-02-06 18:52:13 +00:00
|
|
|
dt = 'VARCHAR(' + (p.limit || 255) + ')';
|
2012-02-20 18:44:02 +00:00
|
|
|
break;
|
2011-12-09 15:23:29 +00:00
|
|
|
case 'Text':
|
2012-02-06 18:52:13 +00:00
|
|
|
dt = 'TEXT';
|
2012-02-20 18:44:02 +00:00
|
|
|
break;
|
2011-12-09 15:23:29 +00:00
|
|
|
case 'Number':
|
2012-02-06 18:52:13 +00:00
|
|
|
dt = 'INT(' + (p.limit || 11) + ')';
|
2012-02-20 18:44:02 +00:00
|
|
|
break;
|
2011-12-09 15:23:29 +00:00
|
|
|
case 'Date':
|
2012-02-06 18:52:13 +00:00
|
|
|
dt = 'DATETIME';
|
2012-02-20 18:44:02 +00:00
|
|
|
break;
|
2011-12-09 15:23:29 +00:00
|
|
|
case 'Boolean':
|
2012-02-06 18:52:13 +00:00
|
|
|
dt = 'TINYINT(1)';
|
2012-02-20 18:44:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return dt;
|
2011-12-09 15:23:29 +00:00
|
|
|
}
|
2012-03-07 07:29:08 +00:00
|
|
|
|