Map GeoPoint to POINT, add more debug information

This commit is contained in:
Raymond Feng 2013-10-07 14:46:05 -07:00
parent f63d164cd9
commit 36884820fc
1 changed files with 24 additions and 3 deletions

View File

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