Skip to content

Commit 039eea1

Browse files
committed
(PDB-5161) engine/parse-dot-query: fix text[] syntax/quoting
Add jdbc/strs->db-array that creates a proper postgres text[] array literal, including backslash escaping every string element, and use us it in parse-dot-query. The previous code was treating the elements as single quoted SQL strings, which doesn't appear to be what postgres expects.
1 parent 004d943 commit 039eea1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/puppetlabs/puppetdb/jdbc.clj

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
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

@@ -392,6 +393,26 @@
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'..."

src/puppetlabs/puppetdb/query_eng/engine.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,9 +1649,7 @@
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

0 commit comments

Comments
 (0)