mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
✨ Add get-with-sql helper to db module
This commit is contained in:
parent
6c7fef29a8
commit
4bdba6894d
1 changed files with 28 additions and 3 deletions
|
@ -10,19 +10,20 @@
|
||||||
[app.common.data :as d]
|
[app.common.data :as d]
|
||||||
[app.common.exceptions :as ex]
|
[app.common.exceptions :as ex]
|
||||||
[app.common.geom.point :as gpt]
|
[app.common.geom.point :as gpt]
|
||||||
|
[app.common.json :as json]
|
||||||
[app.common.logging :as l]
|
[app.common.logging :as l]
|
||||||
[app.common.schema :as sm]
|
[app.common.schema :as sm]
|
||||||
[app.common.transit :as t]
|
[app.common.transit :as t]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[app.db.sql :as sql]
|
[app.db.sql :as sql]
|
||||||
[app.metrics :as mtx]
|
[app.metrics :as mtx]
|
||||||
[app.util.json :as json]
|
|
||||||
[app.util.time :as dt]
|
[app.util.time :as dt]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.set :as set]
|
[clojure.set :as set]
|
||||||
[integrant.core :as ig]
|
[integrant.core :as ig]
|
||||||
[next.jdbc :as jdbc]
|
[next.jdbc :as jdbc]
|
||||||
[next.jdbc.date-time :as jdbc-dt]
|
[next.jdbc.date-time :as jdbc-dt]
|
||||||
|
[next.jdbc.prepare :as jdbc.prepare]
|
||||||
[next.jdbc.transaction])
|
[next.jdbc.transaction])
|
||||||
(:import
|
(:import
|
||||||
com.zaxxer.hikari.HikariConfig
|
com.zaxxer.hikari.HikariConfig
|
||||||
|
@ -33,6 +34,7 @@
|
||||||
java.io.InputStream
|
java.io.InputStream
|
||||||
java.io.OutputStream
|
java.io.OutputStream
|
||||||
java.sql.Connection
|
java.sql.Connection
|
||||||
|
java.sql.PreparedStatement
|
||||||
java.sql.Savepoint
|
java.sql.Savepoint
|
||||||
org.postgresql.PGConnection
|
org.postgresql.PGConnection
|
||||||
org.postgresql.geometric.PGpoint
|
org.postgresql.geometric.PGpoint
|
||||||
|
@ -404,6 +406,24 @@
|
||||||
:hint "database object not found"))
|
:hint "database object not found"))
|
||||||
row))
|
row))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-with-sql
|
||||||
|
[ds sql & {:as opts}]
|
||||||
|
(let [rows (cond->> (exec! ds sql opts)
|
||||||
|
(::remove-deleted opts true)
|
||||||
|
(remove is-row-deleted?)
|
||||||
|
|
||||||
|
:always
|
||||||
|
(not-empty))]
|
||||||
|
|
||||||
|
(when (and (not rows) (::throw-if-not-exists opts true))
|
||||||
|
(ex/raise :type :not-found
|
||||||
|
:code :object-not-found
|
||||||
|
:hint "database object not found"))
|
||||||
|
|
||||||
|
(first rows)))
|
||||||
|
|
||||||
|
|
||||||
(def ^:private default-plan-opts
|
(def ^:private default-plan-opts
|
||||||
(-> default-opts
|
(-> default-opts
|
||||||
(assoc :fetch-size 1000)
|
(assoc :fetch-size 1000)
|
||||||
|
@ -599,7 +619,7 @@
|
||||||
val (.getValue o)]
|
val (.getValue o)]
|
||||||
(if (or (= typ "json")
|
(if (or (= typ "json")
|
||||||
(= typ "jsonb"))
|
(= typ "jsonb"))
|
||||||
(json/decode val)
|
(json/decode val :key-fn keyword)
|
||||||
val))))
|
val))))
|
||||||
|
|
||||||
(defn decode-transit-pgobject
|
(defn decode-transit-pgobject
|
||||||
|
@ -640,7 +660,7 @@
|
||||||
(when data
|
(when data
|
||||||
(doto (org.postgresql.util.PGobject.)
|
(doto (org.postgresql.util.PGobject.)
|
||||||
(.setType "jsonb")
|
(.setType "jsonb")
|
||||||
(.setValue (json/encode-str data)))))
|
(.setValue (json/encode data)))))
|
||||||
|
|
||||||
;; --- Locks
|
;; --- Locks
|
||||||
|
|
||||||
|
@ -686,3 +706,8 @@
|
||||||
[cause]
|
[cause]
|
||||||
(and (sql-exception? cause)
|
(and (sql-exception? cause)
|
||||||
(= "40001" (.getSQLState ^java.sql.SQLException cause))))
|
(= "40001" (.getSQLState ^java.sql.SQLException cause))))
|
||||||
|
|
||||||
|
(extend-protocol jdbc.prepare/SettableParameter
|
||||||
|
clojure.lang.Keyword
|
||||||
|
(set-parameter [^clojure.lang.Keyword v ^PreparedStatement s ^long i]
|
||||||
|
(.setObject s i ^String (d/name v))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue