Monday, April 4, 2016

Update MySQL Module to Support Point Data Type when saving/retriving Geo Location

Out of the box MySQL module does not support point data type, when retrieving/saving data. In order to get it supported, we need to do some modification to the module. This page shows you how to alter MySQL module.



Replace line # 137 of mysql/lib/protocol/SqlString.js code from below code block.

1
2
3
4
5
6
7
8
 // for the mysql point class
    if(value.hasOwnProperty('x') && value.hasOwnProperty('y')) {
       values.push(this.escapeId(key) + ' = ' + 'GeomFromText(\'POINT(' + value.x +' ' + value.y + ')\')');
    }
    else
    {
      values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
    }


Add below function to same file.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SqlString.objectToPointValues = function(object, timeZone) {
  var values = [];
  for (var key in object) {
    var value = object[key];
    if(typeof value === 'function') {
      continue;
    }

    values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
  }

  return values.join(', ');
};


When saving, "point" value of the entity should be formated as follows.

entity.point_field = {x: latVal, y: lngVal}

No comments:

Post a Comment