From d2b7a6a7e2739869f8b718c80cad7c9515f10070 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Thu, 20 Nov 2025 14:08:21 +0100 Subject: Replace nutrient-value alists with hashes everywhere. --- models/fertilizer-product.rkt | 60 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 31 deletions(-) (limited to 'models/fertilizer-product.rkt') diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index 1d6adbb..f9965c2 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -6,17 +6,17 @@ (rename-out [fertilizer-product-canonical-name fertilizer-name] [fertilizer-product-nutrient-values fertilizer-product-values] [fertilizer-product-brand-name fertilizer-brand-name]) - (contract-out - [create-fertilizer-product! - (->* (string? (listof nutrient-value-pair/c)) (string?) fertilizer-product?)] - [get-fertilizer-products (-> (listof fertilizer-product?))] - [get-fertilizer-product - (->* () - (#:id (or/c #f exact-nonnegative-integer?) #:canonical-name (or/c #f string?)) - (or/c fertilizer-product? #f))] - [get-fertilizer-product-values (-> fertilizer-product? (listof nutrient-value-pair/c))] - [get-fertilizer-product-value (-> fertilizer-product? nutrient? number?)] - [delete-fertilizer-product! (-> fertilizer-product? void?)])) + (contract-out [create-fertilizer-product! + (->* (string? nutrient-value-hash/c) (string?) fertilizer-product?)] + [get-fertilizer-products (-> (listof fertilizer-product?))] + [get-fertilizer-product + (->* () + (#:id (or/c #f exact-nonnegative-integer?) + #:canonical-name (or/c #f string?)) + (or/c fertilizer-product? #f))] + [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)] + [get-fertilizer-product-value (-> fertilizer-product? nutrient? number?)] + [delete-fertilizer-product! (-> fertilizer-product? void?)])) (require racket/contract db @@ -37,8 +37,7 @@ (fertilizer-product-canonical-name v) (fertilizer-product-brand-name v)) (fprintf out "~a\n" (fertilizer-product-canonical-name v))) - (for ([nv (in-list (fertilizer-product-nutrient-values v))]) - (match-define (cons n v) nv) + (for ([(n v) (in-hash (fertilizer-product-nutrient-values v))]) (fprintf out "~a ~a\n" (~a (nutrient-name n) #:min-width 14) @@ -65,8 +64,7 @@ (define nvs-id (query-value (current-conn) (select id #:from nutrient_value_sets #:where (= fertilizer_product_id ,fp-id)))) - (for ([nv nutrient-values]) - (match-define (cons n v) nv) + (for ([(n v) (in-hash nutrient-values)]) (query-exec (current-conn) (insert #:into nutrient_values #:set [value_set_id ,nvs-id] @@ -87,22 +85,22 @@ (define (grouped-row->fertilizer-product row) (match-define (vector fp-id canonical-name brand-name residuals) row) - (define nutrient-value-pairs (residuals->nutrient-value-pairs residuals)) - (fertilizer-product fp-id canonical-name nutrient-value-pairs brand-name)) + (fertilizer-product fp-id canonical-name (residuals->nutrient-value-hash residuals) brand-name)) (define (get-fertilizer-products) - (define grouped-rows (query-rows (current-conn) - (select fp.id - fp.canonical_name - fp.brand_name - n.id - n.canonical_name - n.formula - nv.value_ppm - #:from (TableExpr:AST ,joined) - #:order-by fp.canonical_name - #:asc) - #:group '#(0 1 2))) + (define grouped-rows + (query-rows (current-conn) + (select fp.id + fp.canonical_name + fp.brand_name + n.id + n.canonical_name + n.formula + nv.value_ppm + #:from (TableExpr:AST ,joined) + #:order-by fp.canonical_name + #:asc) + #:group '#(0 1 2))) (for/list ([row grouped-rows]) (grouped-row->fertilizer-product row))) @@ -134,7 +132,7 @@ [many (error 'get-fertilizer-product "expected 1 fertilizer product, got ~a" (length many))])) (define (get-fertilizer-product-values fertilizer-product) - (for/list ([(nutrient-id name formula value_ppm) + (for/hash ([(nutrient-id name formula value_ppm) (in-query (current-conn) (select n.id n.canonical_name @@ -142,7 +140,7 @@ nv.value_ppm #:from (TableExpr:AST ,joined) #:where (= fp.id ,(fertilizer-product-id fertilizer-product))))]) - (cons (nutrient nutrient-id name formula) value_ppm))) + (values (nutrient nutrient-id name formula) value_ppm))) (define (get-fertilizer-product-value fertilizer-product nutrient) (query-maybe-value (current-conn) -- cgit v1.2.3