diff --git a/guix-data-service/model/utils.scm b/guix-data-service/model/utils.scm index c05f20d..8721c52 100644 --- a/guix-data-service/model/utils.scm +++ b/guix-data-service/model/utils.scm @@ -145,6 +145,34 @@ WHERE table_name = $1" (string=? is_nullable "YES")))) results)))) +(define %field-can-be-null-cache + (make-hash-table)) + +(define (field-can-be-null? conn table-name field) + (let ((cache-key (cons table-name field))) + (match (hash-get-handle %field-can-be-null-cache + cache-key) + ((_ . res) res) + (#f + (let ((schema-details + (table-schema conn table-name))) + (match (find (lambda (column-data) + (string=? field + (car column-data))) + schema-details) + ((column-name data-type is-nullable?) + (hash-set! %field-can-be-null-cache + cache-key + is-nullable?) + is-nullable?) + (#f + (simple-format + (current-error-port) + "error: couldn't find data for ~A in ~A\n" + field + schema-details) + (error "error: field-can-be-null?")))))))) + (define* (insert-missing-data-and-return-all-ids conn table-name @@ -216,23 +244,6 @@ WHERE table_name = $1" (loop (cdr a) (cdr b)) a-val))))))))))) - (define schema-details - (table-schema conn table-name)) - - (define (field-can-be-null? field) - (match (find (lambda (column-data) - (string=? field - (car column-data))) - schema-details) - ((column-name data-type is-nullable?) is-nullable?) - (#f - (simple-format - (current-error-port) - "error: couldn't find data for ~A in ~A\n" - field - schema-details) - (error "error: field-can-be-null?")))) - (define (select-query data) (string-append "SELECT id,\n" @@ -258,7 +269,7 @@ WHERE table_name = $1" (string-concatenate `("(" ,table-name "." ,field " = vals." ,field - ,@(if (field-can-be-null? field) + ,@(if (field-can-be-null? conn table-name field) `(" OR (" ,table-name "." ,field " IS NULL AND" " vals." ,field " IS NULL" ")") @@ -282,7 +293,7 @@ WHERE table_name = $1" (string-concatenate `("(" ,table-name "." ,field " = " ,temp-table-name "." ,field - ,@(if (field-can-be-null? field) + ,@(if (field-can-be-null? conn table-name field) `(" OR (" ,table-name "." ,field " IS NULL" " AND "