Improve null handling

This commit is contained in:
Christopher Baines 2024-12-01 19:48:44 +00:00
parent bb84e45c42
commit b128e9bd7a
4 changed files with 17 additions and 14 deletions

View file

@ -41,6 +41,8 @@
with-advisory-session-lock/log-time with-advisory-session-lock/log-time
obtain-advisory-transaction-lock obtain-advisory-transaction-lock
NULL
NULL?
exec-query-with-null-handling)) exec-query-with-null-handling))
;; TODO This isn't exported for some reason ;; TODO This isn't exported for some reason
@ -279,6 +281,10 @@
"SELECT pg_advisory_xact_lock($1)" "SELECT pg_advisory_xact_lock($1)"
(list lock-number)))) (list lock-number))))
(define NULL (make-symbol "null"))
(define NULL? (lambda (s) (eq? s NULL)))
(define squee/libpq (define squee/libpq
(@@ (squee) libpq)) (@@ (squee) libpq))
@ -304,7 +310,7 @@
((string-null? val) ((string-null? val)
(if (eq? 1 (%PQgetisnull (if (eq? 1 (%PQgetisnull
(squee/unwrap-result-ptr result-ptr) row-i col-i)) (squee/unwrap-result-ptr result-ptr) row-i col-i))
'() NULL
val)) val))
(else val)))) (else val))))
cols-range)) cols-range))

View file

@ -21,6 +21,7 @@
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (squee) #:use-module (squee)
#:use-module (guix inferior) #:use-module (guix inferior)
#:use-module (guix-data-service database)
#:use-module (guix-data-service model utils) #:use-module (guix-data-service model utils)
#:export (inferior-packages->license-id-lists #:export (inferior-packages->license-id-lists
inferior-packages->license-data)) inferior-packages->license-data))

View file

@ -29,6 +29,7 @@
#:use-module (guix i18n) #:use-module (guix i18n)
#:use-module (guix inferior) #:use-module (guix inferior)
#:use-module (guix-data-service utils) #:use-module (guix-data-service utils)
#:use-module (guix-data-service database)
#:use-module (guix-data-service model location) #:use-module (guix-data-service model location)
#:use-module (guix-data-service model utils) #:use-module (guix-data-service model utils)
#:export (select-package-metadata-by-revision-name-and-version #:export (select-package-metadata-by-revision-name-and-version

View file

@ -23,8 +23,7 @@
#:use-module (squee) #:use-module (squee)
#:use-module (guix-data-service database) #:use-module (guix-data-service database)
#:use-module (guix-data-service utils) #:use-module (guix-data-service utils)
#:export (NULL #:export (quote-string
quote-string
value->quoted-string-or-null value->quoted-string-or-null
non-empty-string-or-false non-empty-string-or-false
exec-query->vhash exec-query->vhash
@ -36,15 +35,13 @@
group-to-alist/vector group-to-alist/vector
insert-missing-data-and-return-all-ids)) insert-missing-data-and-return-all-ids))
(define NULL '())
(define (quote-string s) (define (quote-string s)
(string-append "$STR$" s "$STR$")) (string-append "$STR$" s "$STR$"))
(define (value->quoted-string-or-null value) (define (value->quoted-string-or-null value)
(if (string? value) (if (string? value)
(string-append "$STR$" value "$STR$") (string-append "$STR$" value "$STR$")
"NULL")) NULL))
(define (non-empty-string-or-false s) (define (non-empty-string-or-false s)
(if (string? s) (if (string? s)
@ -189,6 +186,8 @@ WHERE table_name = $1"
(match-lambda (match-lambda
((? string? s) ((? string? s)
(string-append "$STR$" s "$STR$")) (string-append "$STR$" s "$STR$"))
((? NULL?)
"NULL")
((? symbol? s) ((? symbol? s)
(string-append "$STR$" (string-append "$STR$"
(symbol->string s) (symbol->string s)
@ -197,8 +196,6 @@ WHERE table_name = $1"
(number->string n)) (number->string n))
((? boolean? b) ((? boolean? b)
(if b "TRUE" "FALSE")) (if b "TRUE" "FALSE"))
((? null?)
"NULL")
((cast . value) ((cast . value)
(string-append (string-append
(value->sql value) "::" cast)) (value->sql value) "::" cast))
@ -223,11 +220,11 @@ WHERE table_name = $1"
((? symbol? val) (symbol->string val)) ((? symbol? val) (symbol->string val))
(val val)))) (val val))))
(cond (cond
((null? a-val) ((NULL? a-val)
(if (null? b-val) (if (NULL? b-val)
(loop (cdr a) (cdr b)) (loop (cdr a) (cdr b))
#t)) #t))
((null? b-val) ((NULL? b-val)
#f) #f)
(else (else
(match a-val (match a-val
@ -340,13 +337,11 @@ WHERE table_name = $1"
(if b "t" "f")) (if b "t" "f"))
((? number? n) ((? number? n)
(number->string n)) (number->string n))
((? NULL? n) n)
((? symbol? s) ((? symbol? s)
(symbol->string s)) (symbol->string s))
((? string? s) ((? string? s)
s) s)
((? null? n)
;; exec-query-with-null-handling specifies NULL values as '()
n)
((cast . value) ((cast . value)
(if (string=? cast "jsonb") (if (string=? cast "jsonb")
(format-json value) (format-json value)