Support handling jsonb in insert-missing-data-and-return-all-ids

This commit is contained in:
Christopher Baines 2019-11-30 10:53:13 +00:00
parent b6194e7b3d
commit 0fa747e404

View file

@ -106,6 +106,9 @@
(if b "TRUE" "FALSE")) (if b "TRUE" "FALSE"))
((? null?) ((? null?)
"NULL") "NULL")
((cast . value)
(string-append
(value->sql value) "::" cast))
(v (v
(error (error
(simple-format #f "error: unknown type for value: ~A" v))))) (simple-format #f "error: unknown type for value: ~A" v)))))
@ -161,6 +164,15 @@
", ") ", ")
" RETURNING id")) " RETURNING id"))
(define (format-json json)
;; PostgreSQL formats JSON strings differently to guile-json, so use
;; PostgreSQL to do the formatting
(caar
(exec-query
conn
(string-append
"SELECT $STR$" json "$STR$::jsonb"))))
(define (normalise-values data) (define (normalise-values data)
(map (match-lambda (map (match-lambda
((? boolean? b) ((? boolean? b)
@ -174,6 +186,10 @@
((? null? n) ((? null? n)
;; exec-query-with-null-handling specifies NULL values as '() ;; exec-query-with-null-handling specifies NULL values as '()
n) n)
((cast . value)
(if (string=? cast "jsonb")
(format-json value)
value))
(unknown (unknown
(error (simple-format #f "normalise-values: error: ~A\n" unknown)))) (error (simple-format #f "normalise-values: error: ~A\n" unknown))))
data)) data))