Guest User

Untitled

a guest
Jan 11th, 2019
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns anorak.db.core
  2.   (:require
  3.     [clojure.java.jdbc :as jdbc]
  4.     [conman.core :as conman]
  5.     [honeysql.core :as sql]
  6.     [java-time.pre-java8 :as jt]
  7.     [mount.core :refer [defstate]]
  8.     [anorak.config :refer [env]]))
  9.  
  10. (defstate ^:dynamic *db*
  11.           :start (conman/connect! {:jdbc-url (env :database-url)})
  12.           :stop (conman/disconnect! *db*))
  13.  
  14. (conman/bind-connection *db* "sql/queries.sql")
  15.  
  16. (defn list-tables []
  17.   (jdbc/query *db* (sql/format {:select [:name]
  18.                                 :from [:us-census.sqlite_master]
  19.                                 :where [:= :type "table"]})))
  20.  
  21. (extend-protocol jdbc/IResultSetReadColumn
  22.   java.sql.Timestamp
  23.   (result-set-read-column [v _2 _3]
  24.     (.toLocalDateTime v))
  25.   java.sql.Date
  26.   (result-set-read-column [v _2 _3]
  27.     (.toLocalDate v))
  28.   java.sql.Time
  29.   (result-set-read-column [v _2 _3]
  30.     (.toLocalTime v)))
  31.  
  32. (extend-protocol jdbc/ISQLValue
  33.   java.util.Date
  34.   (sql-value [v]
  35.     (java.sql.Timestamp. (.getTime v)))
  36.   java.time.LocalTime
  37.   (sql-value [v]
  38.     (jt/sql-time v))
  39.   java.time.LocalDate
  40.   (sql-value [v]
  41.     (jt/sql-date v))
  42.   java.time.LocalDateTime
  43.   (sql-value [v]
  44.     (jt/sql-timestamp v))
  45.   java.time.ZonedDateTime
  46.   (sql-value [v]
  47.     (jt/sql-timestamp v)))
RAW Paste Data