File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change 1313 (:import
1414 (com.zaxxer.hikari HikariDataSource HikariConfig)
1515 (java.sql Connection SQLException SQLTransientConnectionException)
16- (java.util.concurrent TimeUnit)))
16+ (java.util.concurrent TimeUnit)
17+ (org.postgresql.util PGobject)))
1718
1819(def ^:dynamic *db* nil )
1920
392393 [s]
393394 (str " \" " (escape-double-quotes s) " \" " ))
394395
396+ (defn string->text-array-literal
397+ " Escape string s for inclusion in a postgres text[] literal,
398+ e.g. \" foo\\\" bar\" becomes the \" foo\\\\\\\" bar\" in
399+ '{\" foo\\\\\\\" bar\" }'"
400+ ; ; https://www.postgresql.org/docs/11/arrays.html#ARRAYS-INPUT
401+ ; ; https://www.postgresql.org/docs/11/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
402+ [s]
403+ (assert (string? s))
404+ (str \" (str/replace s " \\ " " \\\\ " ) \"))
405+
406+ (defn strs->db-array
407+ [strs]
408+ (assert (every? string? strs))
409+ ; ; https://www.postgresql.org/docs/11/arrays.html#ARRAYS-INPUT
410+ (let [quoted (map string->text-array-literal strs)]
411+ (doto (PGobject. )
412+ (.setType " text[]" )
413+ (.setValue (str \{ (str/join \, quoted) \})))))
414+
415+ ; ; Q: move/replace?
395416(defn create-json-path-extraction
396417 " Given a base json field and a path of keys to traverse, construct the proper
397418 SQL query of the form base->'key'->'key2'..."
Original file line number Diff line number Diff line change 16491649 ; ; plain integer path components.
16501650 {:node (assoc node :value [" ?" " ?" ] :field column :array-in-path true )
16511651 ; ; https://www.postgresql.org/docs/11/arrays.html#ARRAYS-INPUT
1652- :state (reduce conj state [(doto (PGobject. )
1653- (.setType " text[]" )
1654- (.setValue (str " {" (string/join " ," (map #(string/replace % " '" " ''" ) path)) " }" )))
1652+ :state (reduce conj state [(jdbc/strs->db-array path)
16551653 (su/munge-jsonb-for-storage value)])}
16561654 {:node (assoc node :value " ?" :field column :array-in-path false )
16571655 :state (conj state (su/munge-jsonb-for-storage
You can’t perform that action at this time.
0 commit comments