Merge pull request #3 from strongloop/geo

Map GeoPoint to POINT, add more debug information
This commit is contained in:
Raymond Feng 2013-10-07 20:49:13 -07:00
commit 6686a9d8f9
1 changed files with 31 additions and 3 deletions

View File

@ -47,9 +47,13 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
dataSource.emit('error', err);
});
dataSource.connector = new MySQL(dataSource.client);
if(s.debug) {
console.log('Settings: ', s);
}
dataSource.connector = new MySQL(dataSource.client, s);
dataSource.connector.dataSource = dataSource;
dataSource.client.query('USE `' + s.database + '`', function (err) {
if (err) {
if (err.message.match(/(^|: )unknown database/i)) {
@ -83,10 +87,11 @@ exports.MySQL = MySQL;
* Constructor for MySQL connector
* @param {Object} client The node-mysql client object
*/
function MySQL(client) {
function MySQL(client, settings) {
this.name = 'mysql';
this._models = {};
this.client = client;
this.settings = settings;
}
require('util').inherits(MySQL, jdb.BaseSQL);
@ -105,9 +110,18 @@ MySQL.prototype.query = function (sql, callback) {
}
var client = this.client;
var time = Date.now();
var debug = this.settings.debug;
var log = this.log;
if (typeof callback !== 'function') throw new Error('callback should be a function');
if(debug) {
console.log('SQL:' , sql);
}
this.client.query(sql, function (err, data) {
if(err) {
if(debug) {
console.error('Error:', err);
}
}
if (err && err.message.match(/(^|: )unknown database/i)) {
var dbName = err.message.match(/(^|: )unknown database '(.*?)'/i)[1];
client.query('CREATE DATABASE ' + dbName, function (error) {
@ -119,6 +133,9 @@ MySQL.prototype.query = function (sql, callback) {
});
return;
}
if(debug) {
console.log('Data:' , data);
}
if (log) log(sql, time);
callback(err, data);
});
@ -248,6 +265,9 @@ MySQL.prototype.toDatabase = function (prop, val) {
return '"' + dateToMysql(val) + '"';
}
if (prop.type.name == "Boolean") return val ? 1 : 0;
if (prop.type.name === 'GeoPoint') {
return val ? 'Point(' + val.lat + ',' + val.lng +')' : 'NULL';
}
if (typeof prop.type === 'function') return this.client.escape(prop.type(val));
return this.client.escape(val.toString());
};
@ -274,6 +294,13 @@ MySQL.prototype.fromDatabase = function (model, data) {
case 'Boolean':
val = Boolean(val);
break;
case 'GeoPoint':
case 'Point':
val = {
lat: val.x,
lng: val.y
};
break;
}
}
data[key] = val;
@ -788,6 +815,7 @@ function datatype(p) {
dt = 'TINYINT(1)';
break;
case 'Point':
case 'GeoPoint':
dt = 'POINT';
break;
case 'Enum':