Guest User

Untitled

a guest
Apr 19th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. (ns ___.db
  2. (:require [clojure.java.jdbc :as jdbc]
  3. [clj-time.jdbc] ;; extends JDBC protocols
  4. [honeysql.core :as sql]
  5. [cheshire.core :as json]
  6. [clojure.tools.logging :as log]
  7. [migratus.core :as migratus]
  8. [___.env :refer [env]])
  9. (:import org.postgresql.util.PGobject))
  10.  
  11.  
  12. (def ^:private
  13. db-url
  14. (format "jdbc:postgresql://%s:%s/%s?stringtype=unspecified&user=%s&password=%s"
  15. (:db-host env)
  16. (:db-port env)
  17. (:db-database env)
  18. (:db-user env)
  19. (:db-password env)))
  20.  
  21. (def ^:dynamic
  22. *db* {:dbtype "postgresql"
  23. :connection-uri db-url})
  24.  
  25. ;;
  26. ;; DB types
  27. ;;
  28.  
  29. (defmulti pgobj->clj
  30. (fn [pgobj] (.getType pgobj)))
  31.  
  32. (defmethod pgobj->clj :default
  33. [pgobj]
  34. (-> pgobj .getValue))
  35.  
  36. (defmethod pgobj->clj "jsonb"
  37. [pgobj]
  38. (-> pgobj .getValue (json/parse-string true)))
  39.  
  40. (extend-protocol jdbc/IResultSetReadColumn
  41.  
  42. PGobject
  43. (result-set-read-column [pgobj _metadata _index]
  44. (pgobj->clj pgobj)))
  45.  
  46. ;;
  47. ;; Helpers
  48. ;;
  49.  
  50. (defn to-sql
  51. "Local wrapper to turn a map into a SQL string."
  52. [sqlmap]
  53. (sql/format sqlmap))
  54.  
  55. (def raw sql/raw)
  56.  
  57. (defn model->row
  58. "Turns a model map into its database representation."
  59. [model]
  60. {:id (:id model)
  61. :resource (json/generate-string model)
  62. :deleted false})
  63.  
  64. ;;
  65. ;; DB API
  66. ;; Here and below: partial doesn't work with binding.
  67. ;;
  68.  
  69. (defn query [& args]
  70. (apply jdbc/query *db* args))
  71.  
  72. (defn get-by-id [& args]
  73. (apply jdbc/get-by-id *db* args))
  74.  
  75. (defn find-by-keys [& args]
  76. (apply jdbc/find-by-keys *db* args))
  77.  
  78. (defn insert! [& args]
  79. (first (apply jdbc/insert! *db* args)))
  80.  
  81. (defn insert-multi! [& args]
  82. (apply jdbc/insert-multi! *db* args))
  83.  
  84. (defn update! [& args]
  85. (apply jdbc/update! *db* args))
  86.  
  87. (defn delete! [& args]
  88. (apply jdbc/delete! *db* args))
  89.  
  90. (defn execute! [& args]
  91. (apply jdbc/execute! *db* args))
  92.  
  93. (defmacro with-tx
  94. "Runs a series of queries into transaction."
  95. [& body]
  96. `(jdbc/with-db-transaction [tx# *db*]
  97. (binding [*db* tx#]
  98. ~@body)))
  99.  
  100. (defmacro with-tx-test
  101. "The same as `with-tx` but rolls back the transaction after all."
  102. [& body]
  103. `(with-tx
  104. (jdbc/db-set-rollback-only! *db*)
  105. ~@body))
  106.  
  107. ;;
  108. ;; Custom queries
  109. ;;
  110.  
  111. (defn- query-insert-models
  112. [table rows]
  113. (let [query-map {:insert-into table
  114. :values rows}
  115. extra "ON CONFLICT (id) DO UPDATE SET deleted = EXCLUDED.deleted, resource = EXCLUDED.resource"
  116. query-vect (sql/format query-map)
  117. query-main (first query-vect)
  118. query-full (format "%s %s" query-main extra)]
  119. (into [query-full] (rest query-vect))))
  120.  
  121. (def query-insert-practitioners
  122. (partial query-insert-models :practitioner))
  123.  
  124. (def query-insert-organizations
  125. (partial query-insert-models :organizations))
  126.  
  127. (defn- query-delete-models
  128. [table npis]
  129. (to-sql {:update table
  130. :set {:deleted true}
  131. :where [:in :id npis]}))
  132.  
  133. (def query-delete-practitioners
  134. (partial query-delete-models :practitioner))
  135.  
  136. (def query-delete-organizations
  137. (partial query-delete-models :organizations))
  138.  
  139. ;;
  140. ;; migrations
  141. ;;
  142.  
  143. (def ^:private
  144. mg-cfg {:store :database
  145. :migration-dir "migrations"
  146. :db *db*})
  147.  
  148. (defn- migrate []
  149. (log/info "Running migrations...")
  150. (migratus/migrate mg-cfg)
  151. (log/info "Migrations done."))
  152.  
  153. ;;
  154. ;; Init part
  155. ;;
  156.  
  157. (defn init []
  158. (migrate))
Add Comment
Please, Sign In to add comment