Hello! I was looking for a query builder for PostGIS and was directed to HoneySQL. I understand that there is no PostGIS support but might something like this work through JDBC?
(def postgis-map {:select [:city/name :state/name :city/geom]
:from [:city]
:join [:state (sql/raw ["ST_Intersects(" :city/geom ", " :state/geom ")"])]})
(sql/format postgis-map :namespace-as-table? true)
;; => ["SELECT city.name, state.name, city.geom FROM city INNER JOIN state ON ST_Intersects(city.geom, state.geom)"]
Am trying to see if I can compose PostGIS queries by simply using sql/raw
and sql/forma
which seems to work 🙂
Any advice on how I might want to improve my approach to this? And just wondering if there is anything I should be aware of when linking up to JDBC to PostgreSQL + PostGIS (extension)That's the approach I take
It works fine
With honeysql v2, out in due course, it becomes a lot easier
i.e.,
(defn location->st-point
[{:keys [lat lng] :as location}]
[[:ST_SetSRID [:ST_MakePoint (round6 lng) (round6 lat)] [:cast 4326 :integer]]])
anything starting with a :
is interpreted as a function call
(that is in honeysql v2 syntax)
Cool, excited to get this working! 🙂
On a sidenote, is there a place I can look at the unstable(?) implementation of honeysql v2?
yes
seancorfield/honeysql {:mvn/version "2.0.0-alpha1"}
and https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha1/doc/readme
@zackteo Even 1.x supports PostGIS expressions -- there's a section in the README about it: https://github.com/seancorfield/honeysql/#postgis /cc @dharrigan
(and, yes, 2.x makes this even easier)