aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/model/license-set.scm
blob: 0ff253e7a4100c2f9893fc21a932a46ea10011a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(define-module (guix-data-service model license-set)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 vlist)
  #:use-module (squee)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service model license)
  #:export (inferior-packages->license-set-ids))

(define select-license-sets
  "
SELECT id, license_ids
FROM license_sets")

(define (insert-license-sets license-id-lists)
  (string-append
   "INSERT INTO license_sets (license_ids) VALUES "
   (string-join
    (map (lambda (license-ids)
           (string-append
            "('{"
            (string-join
             (map number->string
                  (sort license-ids <))
             ", ")
            "}')"))
         license-id-lists)
    ", ")
   " RETURNING id"))

(define (inferior-packages->license-set-ids conn inf packages)
  (define license-id-lists
    (inferior-packages->license-id-lists conn inf packages))

  (let* ((unique-license-id-lists (delete-duplicates
                                   license-id-lists))
         (existing-license-sets
          (exec-query->vhash conn
                             select-license-sets
                             (lambda (results)
                               (if (string=? (second results) "{}")
                                   '()
                                   (map
                                    string->number
                                    (string-split
                                     (string-drop-right
                                      (string-drop (second results) 1)
                                      1)
                                     #\,))))
                             (lambda (result)
                               (string->number (first result))))) ;; id
         (missing-license-sets
          (delete-duplicates
           (filter (lambda (license-set-license-ids)
                     (not (vhash-assoc license-set-license-ids
                                       existing-license-sets)))
                   unique-license-id-lists)))
         (new-license-set-entries
          (if (null? missing-license-sets)
              '()
              (map (lambda (result)
                     (string->number (first result)))
                   (exec-query conn
                               (insert-license-sets missing-license-sets)))))
         (new-entries-id-lookup-vhash
          (two-lists->vhash missing-license-sets
                            new-license-set-entries)))

    (map (lambda (license-id-list)
           (cdr
            (or (vhash-assoc license-id-list
                             existing-license-sets)
                (vhash-assoc license-id-list
                             new-entries-id-lookup-vhash)
                (begin
                  (error "missing license set entry"
                         license-id-list)))))
         license-id-lists)))