Skip to content

Commit

Permalink
add support for postgis geometry columns
Browse files Browse the repository at this point in the history
  • Loading branch information
quidquid committed Aug 16, 2018
1 parent 1ee389d commit aa36cd9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/pgsql/sql/list-typenames-without-btree-support.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ select typname,
join pg_type t on c.opcintype = t.oid
where amname = 'btree' and t.oid = pg_type.oid
)
group by typname;
group by typname
union
select 'geometry', 'gist'
union
select 'geometry(Geometry,4326)', 'gist';
10 changes: 7 additions & 3 deletions src/sources/mysql/mysql-cast-rules.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@
:target (:type ,#'enum-or-set-name)
:using pgloader.transforms::set-to-enum-array)

;; geometric data types, just POINT for now
;; geometric data types
(:source (:type "geometry")
:target (:type "point")
:using pgloader.transforms::convert-mysql-point)
:target (:type "geometry(Geometry,4326)")
:using pgloader.transforms::convert-mysql-geometry)

(:source (:type "polygon")
:target (:type "geometry(Geometry,4326)")
:using pgloader.transforms::convert-mysql-geometry)

(:source (:type "point")
:target (:type "point")
Expand Down
11 changes: 6 additions & 5 deletions src/sources/mysql/mysql-schema.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,12 @@
Mostly we just use the name, but in case of POINT we need to use
astext(name)."
(case (intern (string-upcase type) "KEYWORD")
(:geometry (format nil "astext(`~a`) as `~a`" name name))
(:point (format nil "astext(`~a`) as `~a`" name name))
(:linestring (format nil "astext(`~a`) as `~a`" name name))
(t (format nil "`~a`" name))))
(:point (format nil "AsText(`~a`) as `~a`" name name))
(:linestring (format nil "AsText(`~a`) as `~a`" name name))
(:geometry (format nil "AsText(`~a`) as `~a`" name name))
(:polygon (format nil "AsText(`~a`) as `~a`" name name))
(:multipolygon (format nil "AsText(`~a`) as `~a`" name name))
(t (format nil "`~a`" name))))

(defun get-column-list (copy)
"Some MySQL datatypes have a meaningless default output representation, we
Expand Down Expand Up @@ -289,4 +291,3 @@
collect (cond (is-null :null)
(is-empty "")
(t current-col))))

9 changes: 9 additions & 0 deletions src/utils/transforms.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@
(ip-end (int-to-ip (parse-integer end-integer-string))))
(concatenate 'simple-base-string ip-start "-" ip-end))))

(defun convert-mysql-geometry (mysql-geometry-as-string)
"Transform the MYSQL-GEOMETRY-AS-STRING into a suitable representation for
PostgreSQL.
See https://stackoverflow.com/questions/10216891/postgresql-copy-from-stdin-expressions
Input: \"POLYGON((-83.04104755426674 42.32826129762502,-83.04236386207239 42.327690590828354,-83.04251545570035 42.327885465277404,-83.04219557243864 42.328008057710655,-83.04110621668848 42.32833681323786,-83.04104755426674 42.32826129762502))\" ; that's using astext(column)
Output: \"SRID=4326;POLYGON((-83.0410475542667 42.328261297625,-83.0423638620724 42.3276905908284,-83.0425154557003 42.3278854652774,-83.0421955724386 42.3280080577106,-83.0411062166885 42.3283368132379,-83.0410475542667 42.328261297625))\""
(format nil "SRID=4326;~a" mysql-geometry-as-string))

(defun convert-mysql-point (mysql-point-as-string)
"Transform the MYSQL-POINT-AS-STRING into a suitable representation for
PostgreSQL.
Expand Down

0 comments on commit aa36cd9

Please sign in to comment.