From 0fa747e404b1baebf3c733a1554959e122c7ab25 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sat, 30 Nov 2019 10:53:13 +0000 Subject: Support handling jsonb in insert-missing-data-and-return-all-ids --- guix-data-service/model/utils.scm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guix-data-service/model/utils.scm b/guix-data-service/model/utils.scm index a1cd432..c17224b 100644 --- a/guix-data-service/model/utils.scm +++ b/guix-data-service/model/utils.scm @@ -106,6 +106,9 @@ (if b "TRUE" "FALSE")) ((? null?) "NULL") + ((cast . value) + (string-append + (value->sql value) "::" cast)) (v (error (simple-format #f "error: unknown type for value: ~A" v))))) @@ -161,6 +164,15 @@ ", ") " 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) (map (match-lambda ((? boolean? b) @@ -174,6 +186,10 @@ ((? null? n) ;; exec-query-with-null-handling specifies NULL values as '() n) + ((cast . value) + (if (string=? cast "jsonb") + (format-json value) + value)) (unknown (error (simple-format #f "normalise-values: error: ~A\n" unknown)))) data)) -- cgit v1.2.3