diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-10-20 20:41:07 +0200 | 
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-10-20 20:41:07 +0200 | 
| commit | fddbcb3923d3dd019d07b3f37c033fab36c2cadc (patch) | |
| tree | 87a185fe72e0c57e663677a004d7d24ef31fa314 /models | |
| parent | 9620cae7bba35503a2ea4679938f76749a34245a (diff) | |
Simplify get-* model accessors.
Diffstat (limited to 'models')
| -rw-r--r-- | models/crop-requirement.rkt | 30 | ||||
| -rw-r--r-- | models/crop.rkt | 27 | ||||
| -rw-r--r-- | models/fertilizer-product.rkt | 28 | ||||
| -rw-r--r-- | models/nutrient-measurement.rkt | 10 | ||||
| -rw-r--r-- | models/nutrient-target.rkt | 10 | ||||
| -rw-r--r-- | models/nutrient.rkt | 28 | 
6 files changed, 32 insertions, 101 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt index f213441..d1c6ddd 100644 --- a/models/crop-requirement.rkt +++ b/models/crop-requirement.rkt @@ -13,12 +13,7 @@                                             number?)))                                   ((or/c #f crop?))                                   crop-requirement?)] -  [get-crop-requirements (->* () -                              (#:id -                               (or/c #f exact-nonnegative-integer?) -                               #:profile -                               (or/c #f string?)) -                              (listof crop-requirement?))] +  [get-crop-requirements (-> (listof crop-requirement?))]    [get-crop-requirement (->* ()                               (#:id                                (or/c #f exact-nonnegative-integer?) @@ -90,27 +85,16 @@  ;; READ -(define (get-crop-requirements #:id [id #f] -                                   #:profile [profile #f]) -  (define (where-expr) -    (define clauses -      (filter values -              (list (and id (format "id = ~e" id)) -                    (and profile (format "profile = ~e" profile))))) -    (cond -      [(null? clauses) ""] -      [else (format "WHERE ~a" (string-join clauses " AND "))])) -  (define query (string-join -                 `("SELECT id, profile" -                   "FROM crop_requirements" -                   ,(where-expr) -                   "ORDER BY id ASC"))) +(define (get-crop-requirements)    (for/list ([(id* profile*) -              (in-query (current-conn) query)]) +              (in-query (current-conn) +                        (select id profile +                                #:from crop_requirements +                                #:order-by id #:asc))])      (crop-requirement id* profile*)))  (define (get-crop-requirement #:id [id #f] -                                  #:profile [profile #f]) +                              #:profile [profile #f])    (define (where-expr)      (define clauses        (filter values diff --git a/models/crop.rkt b/models/crop.rkt index 7163cbd..f77cbaa 100644 --- a/models/crop.rkt +++ b/models/crop.rkt @@ -8,10 +8,7 @@   ;; SQL CRUD   (contract-out    [create-crop! (-> string? void?)] -  [get-crops (->* () -                  (#:id      (or/c #f exact-nonnegative-integer?) -                   #:name    (or/c #f string?)) -                  (listof crop?))] +  [get-crops (-> (listof crop?))]    [get-crop (->* ()                   (#:id      (or/c #f exact-nonnegative-integer?)                    #:name    (or/c #f string?)) @@ -39,24 +36,12 @@  ;; READ -(define (get-crops #:id [id #f] -                   #:name [name #f]) -  (define (where-expr) -    (define clauses -      (filter values -              (list -               (and id (format "id = ~e" id)) -               (and name (format "canonical_name = ~e" name))))) -    (cond -      [(null? clauses) ""] -      [else (format "WHERE ~a" (string-join clauses " AND "))])) -  (define query (string-join -                 `("SELECT id, canonical_name" -                   "FROM crops" -                   ,(where-expr) -                   "ORDER BY id ASC"))) +(define (get-crops)    (for/list ([(id* name*) -              (in-query (current-conn) query)]) +              (in-query (current-conn) +                        (select id canonical_name +                                #:from crops +                                #:order-by id #:asc))])      (crop id* name*)))  (define (get-crop #:id [id #f] diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index 254ce35..073ef4f 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -13,12 +13,7 @@                                               number?)))                                     (string?)                                     fertilizer-product?)] -  [get-fertilizer-products (->* () -                                (#:id -                                 (or/c #f exact-nonnegative-integer?) -                                 #:brand-name -                                 (or/c #f string?)) -                                (listof fertilizer-product?))] +  [get-fertilizer-products (-> (listof fertilizer-product?))]    [get-fertilizer-product (->* ()                                 (#:id                                  (or/c #f exact-nonnegative-integer?) @@ -85,23 +80,12 @@  ;; READ -(define (get-fertilizer-products #:id [id #f] -                                   #:brand-name [brand-name #f]) -  (define (where-expr) -    (define clauses -      (filter values -              (list (and id (format "id = ~e" id)) -                    (and brand-name (format "brand_name = ~e" brand-name))))) -    (cond -      [(null? clauses) ""] -      [else (format "WHERE ~a" (string-join clauses " AND "))])) -  (define query (string-join -                 `("SELECT id, brand_name" -                   "FROM fertilizer_products" -                   ,(where-expr) -                   "ORDER BY id ASC"))) +(define (get-fertilizer-products)    (for/list ([(id* brand-name*) -              (in-query (current-conn) query)]) +              (in-query (current-conn) +                        (select id brand_name +                                #:from fertilizer_products +                                #:order-by canonical_name #:asc))])      (fertilizer-product id* brand-name*)))  (define (get-fertilizer-product #:id [id #f] diff --git a/models/nutrient-measurement.rkt b/models/nutrient-measurement.rkt index 28e3bdc..d1bd534 100644 --- a/models/nutrient-measurement.rkt +++ b/models/nutrient-measurement.rkt @@ -12,8 +12,7 @@                                               nutrient?                                               number?))                                      nutrient-measurement?)] -  [get-nutrient-measurements (-> void? -                                 (listof nutrient-measurement?))] +  [get-nutrient-measurements (-> (listof nutrient-measurement?))]    [get-nutrient-measurement (->* ()                                   (#:id          (or/c #f exact-nonnegative-integer?)                                    #:measured-on (or/c #f string?)) @@ -81,10 +80,9 @@  (define (get-nutrient-measurements)    (for/list ([(id* measured-on*)                (in-query (current-conn) -                        (string-join -                         `("SELECT id, measured_on" -                           "FROM nutrient_measurements" -                           "ORDER BY id ASC")))]) +                        (select id measured_on +                                #:from nutrient_measurements +                                #:order-by measured_on #:asc))])      (nutrient-measurement id* measured-on*)))  (define (get-nutrient-measurement #:id [id #f] diff --git a/models/nutrient-target.rkt b/models/nutrient-target.rkt index e9c168e..29ea52b 100644 --- a/models/nutrient-target.rkt +++ b/models/nutrient-target.rkt @@ -12,8 +12,7 @@                                          nutrient?                                          number?))                                 nutrient-target?)] -  [get-nutrient-targets (-> void? -                            (listof nutrient-target?))] +  [get-nutrient-targets (-> (listof nutrient-target?))]    [get-nutrient-target (->* ()                              (#:id                               (or/c #f exact-nonnegative-integer?) @@ -84,10 +83,9 @@  (define (get-nutrient-targets)    (for/list ([(id* effective-on*)                (in-query (current-conn) -                        (string-join -                         `("SELECT id, effective_on" -                           "FROM nutrient_targets" -                           "ORDER BY id ASC")))]) +                        (select id effective_on +                                #:from nutrient_targets +                                #:order-by id ASC))])      (nutrient-target id* effective-on*)))  (define (get-nutrient-target #:id [id #f] diff --git a/models/nutrient.rkt b/models/nutrient.rkt index 5a32e70..f2ef4fd 100644 --- a/models/nutrient.rkt +++ b/models/nutrient.rkt @@ -8,11 +8,7 @@   ;; SQL CRUD   (contract-out    [create-nutrient! (-> string? string? void?)] -  [get-nutrients (->* () -                      (#:id      (or/c #f exact-nonnegative-integer?) -                       #:name    (or/c #f string?) -                       #:formula (or/c #f string?)) -                      (listof nutrient?))] +  [get-nutrients (-> (listof nutrient?))]    [get-nutrient (->* ()                       (#:id      (or/c #f exact-nonnegative-integer?)                        #:name    (or/c #f string?) @@ -42,26 +38,12 @@  ;; READ -(define (get-nutrients #:id [id #f] -                       #:name [name #f] -                       #:formula [formula #f]) -  (define (where-expr) -    (define clauses -      (filter values -              (list -               (and id (format "id = ~e" id)) -               (and name (format "canonical_name = ~e" name)) -               (and formula (format "formula = ~e" formula))))) -    (cond -      [(null? clauses) ""] -      [else (format "WHERE ~a" (string-join clauses " AND "))])) +(define (get-nutrients)    (for/list ([(id* name* formula*)                (in-query (current-conn) -                        (string-join -                         `("SELECT id, canonical_name, formula" -                           "FROM nutrients" -                           ,(where-expr) -                           "ORDER BY id ASC")))]) +                        (select id canonical_name formula +                                #:from nutrients +                                #:order-by id #:asc))])      (nutrient id* name* formula*)))  (define (get-nutrient #:id [id #f]  |