cbaines pushed a commit to branch master
in repository data-service.

commit b128e9bd7a2cf896a396f5d29af05ca2a852c829
Author: Christopher Baines <[email protected]>
AuthorDate: Sun Dec 1 19:48:44 2024 +0000

    Improve null handling
---
 guix-data-service/database.scm               |  8 +++++++-
 guix-data-service/model/license.scm          |  1 +
 guix-data-service/model/package-metadata.scm |  1 +
 guix-data-service/model/utils.scm            | 21 ++++++++-------------
 4 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/guix-data-service/database.scm b/guix-data-service/database.scm
index 8af53da..88d1172 100644
--- a/guix-data-service/database.scm
+++ b/guix-data-service/database.scm
@@ -41,6 +41,8 @@
             with-advisory-session-lock/log-time
             obtain-advisory-transaction-lock
 
+            NULL
+            NULL?
             exec-query-with-null-handling))
 
 ;; TODO This isn't exported for some reason
@@ -279,6 +281,10 @@
                 "SELECT pg_advisory_xact_lock($1)"
                 (list lock-number))))
 
+(define NULL (make-symbol "null"))
+
+(define NULL? (lambda (s) (eq? s NULL)))
+
 (define squee/libpq
   (@@ (squee) libpq))
 
@@ -304,7 +310,7 @@
              ((string-null? val)
               (if (eq? 1 (%PQgetisnull
                           (squee/unwrap-result-ptr result-ptr) row-i col-i))
-                  '()
+                  NULL
                   val))
              (else val))))
         cols-range))
diff --git a/guix-data-service/model/license.scm 
b/guix-data-service/model/license.scm
index f0c9e21..452a97b 100644
--- a/guix-data-service/model/license.scm
+++ b/guix-data-service/model/license.scm
@@ -21,6 +21,7 @@
   #:use-module (ice-9 match)
   #:use-module (squee)
   #:use-module (guix inferior)
+  #:use-module (guix-data-service database)
   #:use-module (guix-data-service model utils)
   #:export (inferior-packages->license-id-lists
             inferior-packages->license-data))
diff --git a/guix-data-service/model/package-metadata.scm 
b/guix-data-service/model/package-metadata.scm
index 9668185..8f5643c 100644
--- a/guix-data-service/model/package-metadata.scm
+++ b/guix-data-service/model/package-metadata.scm
@@ -29,6 +29,7 @@
   #:use-module (guix i18n)
   #:use-module (guix inferior)
   #: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 utils)
   #:export (select-package-metadata-by-revision-name-and-version
diff --git a/guix-data-service/model/utils.scm 
b/guix-data-service/model/utils.scm
index 3fda78f..2f048fb 100644
--- a/guix-data-service/model/utils.scm
+++ b/guix-data-service/model/utils.scm
@@ -23,8 +23,7 @@
   #:use-module (squee)
   #:use-module (guix-data-service database)
   #:use-module (guix-data-service utils)
-  #:export (NULL
-            quote-string
+  #:export (quote-string
             value->quoted-string-or-null
             non-empty-string-or-false
             exec-query->vhash
@@ -36,15 +35,13 @@
             group-to-alist/vector
             insert-missing-data-and-return-all-ids))
 
-(define NULL '())
-
 (define (quote-string s)
   (string-append "$STR$" s "$STR$"))
 
 (define (value->quoted-string-or-null value)
   (if (string? value)
       (string-append "$STR$" value "$STR$")
-      "NULL"))
+      NULL))
 
 (define (non-empty-string-or-false s)
   (if (string? s)
@@ -189,6 +186,8 @@ WHERE table_name = $1"
     (match-lambda
       ((? string? s)
        (string-append "$STR$" s "$STR$"))
+      ((? NULL?)
+       "NULL")
       ((? symbol? s)
        (string-append "$STR$"
                       (symbol->string s)
@@ -197,8 +196,6 @@ WHERE table_name = $1"
        (number->string n))
       ((? boolean? b)
        (if b "TRUE" "FALSE"))
-      ((? null?)
-       "NULL")
       ((cast . value)
        (string-append
         (value->sql value) "::" cast))
@@ -223,11 +220,11 @@ WHERE table_name = $1"
                             ((? symbol? val) (symbol->string val))
                             (val val))))
                (cond
-                ((null? a-val)
-                 (if (null? b-val)
+                ((NULL? a-val)
+                 (if (NULL? b-val)
                      (loop (cdr a) (cdr b))
                      #t))
-                ((null? b-val)
+                ((NULL? b-val)
                  #f)
                 (else
                  (match a-val
@@ -340,13 +337,11 @@ WHERE table_name = $1"
             (if b "t" "f"))
            ((? number? n)
             (number->string n))
+           ((? NULL? n) n)
            ((? symbol? s)
             (symbol->string s))
            ((? string? s)
             s)
-           ((? null? n)
-            ;; exec-query-with-null-handling specifies NULL values as '()
-            n)
            ((cast . value)
             (if (string=? cast "jsonb")
                 (format-json value)

Reply via email to