mirror of
https://gitlab.com/wheres-the-tp/server.git
synced 2026-01-25 07:54:57 -06:00
move SQL out of code using HugSQL
This commit is contained in:
parent
16bcc4de5d
commit
923014d563
4 changed files with 41 additions and 59 deletions
|
|
@ -13,7 +13,8 @@
|
|||
[ring-middleware-format "0.7.2"]
|
||||
[org.clojure/data.json "0.2.6"]
|
||||
[ring/ring-json "0.4.0"]
|
||||
[ring/ring-jetty-adapter "1.4.0"]]
|
||||
[ring/ring-jetty-adapter "1.4.0"]
|
||||
[com.layerware/hugsql "0.4.8"]]
|
||||
:plugins [[lein-ring "0.9.7"] [lein-environ "1.1.0"]]
|
||||
:ring {:handler aretherecookies.app/app}
|
||||
:profiles
|
||||
|
|
|
|||
|
|
@ -1,66 +1,32 @@
|
|||
(ns aretherecookies.db
|
||||
(:require
|
||||
[clojure.java.jdbc :as jdbc]
|
||||
(:require [clojure.java.jdbc :as jdbc]
|
||||
[environ.core :refer [env]]
|
||||
[clojure.string :as str])
|
||||
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
|
||||
[clojure.string :as str]
|
||||
[hugsql.core :as hugsql])
|
||||
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
|
||||
|
||||
(def db-spec {
|
||||
:uri (env :database-jdbc-uri)
|
||||
:password (env :database-password)
|
||||
:user (env :database-user)
|
||||
:name (env :database-name)
|
||||
})
|
||||
(hugsql/def-db-fns "aretherecookies/queries.sql")
|
||||
|
||||
(def db-spec {:uri (env :database-jdbc-uri)
|
||||
:password (env :database-password)
|
||||
:user (env :database-user)
|
||||
:name (env :database-name)})
|
||||
|
||||
(defn pool
|
||||
[spec]
|
||||
(let [cpds (doto (ComboPooledDataSource.)
|
||||
(.setDriverClass "org.postgresql.Driver")
|
||||
(.setJdbcUrl (str "jdbc:" (:uri spec)))
|
||||
(.setUser (:user spec))
|
||||
(.setPassword (:password spec))
|
||||
[spec]
|
||||
(let [cpds (doto (ComboPooledDataSource.)
|
||||
(.setDriverClass "org.postgresql.Driver")
|
||||
(.setJdbcUrl (str "jdbc:" (:uri spec)))
|
||||
(.setUser (:user spec))
|
||||
(.setPassword (:password spec))
|
||||
;; expire excess connections after 30 minutes of inactivity:
|
||||
(.setMaxIdleTimeExcessConnections (* 30 60))
|
||||
(.setMaxIdleTimeExcessConnections (* 30 60))
|
||||
;; expire connections after 3 hours of inactivity:
|
||||
(.setMaxIdleTime (* 3 60 60)))]
|
||||
(.setMaxIdleTime (* 3 60 60)))]
|
||||
(println (str "jdbc:" (:url spec)))
|
||||
{:datasource cpds}))
|
||||
|
||||
(def pooled-db (delay (pool db-spec)))
|
||||
(defonce pooled-db (delay (pool db-spec)))
|
||||
|
||||
(def location-query "
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
place_id,
|
||||
category,
|
||||
images,
|
||||
thumbImage,
|
||||
ST_AsGeoJSON(loc) as location,
|
||||
ST_Distance(
|
||||
loc,
|
||||
@LOCATION@
|
||||
) / 1609 as distance
|
||||
FROM food_items
|
||||
WHERE
|
||||
ST_DWithin(
|
||||
loc,
|
||||
@LOCATION@,
|
||||
@DISTANCE@ * 1609
|
||||
)
|
||||
ORDER BY distance ASC;")
|
||||
|
||||
(defn point-text [lng lat]
|
||||
(->
|
||||
"ST_GeogFromText('SRID=4326;POINT(@LONGITUDE@ @LATITUDE@)')"
|
||||
(str/replace "@LONGITUDE@" (str lng))
|
||||
(str/replace "@LATITUDE@" (str lat))))
|
||||
|
||||
(defn build-fooditems-query [lng lat dist]
|
||||
(->
|
||||
location-query
|
||||
(str/replace "@LOCATION@" (point-text (str lng) (str lat)))
|
||||
(str/replace "@DISTANCE@" (str dist))))
|
||||
|
||||
(defn query-food-items [query]
|
||||
(jdbc/query @pooled-db [query]))
|
||||
(defn query-food-items-by-distance [{:keys [lat lng radius]}]
|
||||
(food-items-by-distance @pooled-db {:dist radius :lng lng :lat lat}))
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
(ns aretherecookies.handler
|
||||
(:require [aretherecookies.db :refer [query-food-items build-fooditems-query]]
|
||||
(:require [aretherecookies.db :refer [query-food-items-by-distance]]
|
||||
[compojure.core :refer :all]
|
||||
[compojure.route :as route]
|
||||
[clojure.data.json :as json]
|
||||
|
|
@ -41,8 +41,10 @@
|
|||
(hash-map
|
||||
:orderby orderby
|
||||
:filter filter
|
||||
:fooditems (food-items-to-json
|
||||
(query-food-items (build-fooditems-query lng lat (get filter "radius")))))
|
||||
:fooditems (food-items-to-json
|
||||
(query-food-items-by-distance {:lat lat
|
||||
:lng lng
|
||||
:radius (get filter "radius")})))
|
||||
:value-fn uuid-to-string)))
|
||||
|
||||
(defroutes app-routes
|
||||
|
|
|
|||
13
src/aretherecookies/queries.sql
Normal file
13
src/aretherecookies/queries.sql
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-- :name food-items-by-distance :raw
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
place_id,
|
||||
category,
|
||||
images,
|
||||
thumbImage,
|
||||
ST_AsGeoJSON(loc) as location,
|
||||
ST_Distance(loc, ST_SetSRID(ST_Point(:lng, :lat),4326)::geography) / 1609 as distance
|
||||
FROM food_items
|
||||
WHERE ST_DWithin(loc, ST_SetSRID(ST_Point(:lng, :lat),4326)::geography, :dist * 1609)
|
||||
ORDER BY distance ASC
|
||||
Loading…
Add table
Reference in a new issue