I’m trying to manually manage some geometry (spatial) columns in a rails model.
When updating the geometry column I do this in rails:
self.geom="POINTFROMTEXT('POINT(#{lat},#{lng})')"
Which is the value I want to be in the SQL updates and so be evaluated by the database. However by the time this has been through the active record magic, it comes out as:
INSERT INTO `places` (..., `geom`) VALUES(...,'POINTFROMTEXT(\'POINT(52.2531519,20.9778386)\')')
In other words, the quotes are escaped. This is fine for the other columns as it prevents sql-injection, but not for this. The values are guaranteed to be floats, and I want the update to look like:
INSERT INTO `places` (..., `geom`) VALUES(...,'POINTFROMTEXT('POINT(52.2531519,20.9778386)')')
So is there a way to turn escaping off for a particular column? Or a better way to do this?
(I’ve tried using GeoRuby+spatial adapter, and spatial adaptor seems too buggy to me, plus I don’t need all the functionality – hence trying to do it directly).
The Rails Spatial Adapter should implement exactly what you need. Although, before I found GeoRuby & Spatial Adapter, I was doing this:
On a
after_savehook, I ran something like this:connection.execute “update mytable set geom_column=#{text_column} where id=#{id}”
But the solution above was just a hack, and this have additional issues: I can’t create a spatial index if the column allows
NULLvalues, MySQL doesn’t let me set a default value on a geometry column, and thesavemethod fails if the geometry column doesn’t have a value set.So I would try GeoRuby & Spatial Adapter instead, or reuse some of its code (on my case, I am considering extracting only the GIS-aware
MysqlAdapter#quotemethod from the Spatial Adapter code).