I referenced irbanana's answer about supporting Spatial data type for PostGIS. I'm using MySQL and am trying to implement Value()
for the custom data type EWKBGeomPoint
.
My Gorm model:
import (
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
)
type EWKBGeomPoint geom.Point
type Tag struct {
Name string `json:"name"`
json:"siteID"` // forign key
Loc EWKBGeomPoint `json:"loc"`
}
From what I know, MySQL supports insertion like this:
INSERT INTO `tag` (`name`,`loc`) VALUES ('tag name',ST_GeomFromText('POINT(10.000000 20.000000)'))
or
INSERT INTO `tag` (`name`,`loc`) VALUES ('tag name', ST_GeomFromWKB(X'0101000000000000000000F03F000000000000F03F'))
If I do a my own Value()
to satisfy the database/sql
's Valuer
interface:
func (g EWKBGeomPoint) Value() (driver.Value, error) {
log.Println("EWKBGeomPoint value called")
b := geom.Point(g)
bp := &b
floatArr := bp.Coords()
return fmt.Sprintf("ST_GeomFromText('POINT(%f %f)')", floatArr[0], floatArr[1]), nil
}
The entire value including ST_GeomFromText()
is quoted in a single quote from Gorm, and so it won't work:
INSERT INTO `tag` (`name`,`loc`) VALUES ('tag name','ST_GeomFromText('POINT(10.000000 20.000000)')');
How do I make it work?
EDIT 1:
I trace into Gorm code, eventually it get's to callback_create.go
's createCallback
function. Inside it check for if primaryField == nil
and it is true, it goes into calling scope.SQLDB().Exec
then I failed to trace further.
scope.SQL is string INSERT INTO
tag(
name,
loc) VALUES (?,?)
and scope.SQLVars
prints [tag name {{1 2 [10 20] 0}}]
. It looks like interpolation happens inside this call.
Is this calling into database/sql
code?
EDIT 2:
Found a similar Stackoverflow question here. But I do not understand the solution.