aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/model/utils.scm
diff options
context:
space:
mode:
Diffstat (limited to 'guix-data-service/model/utils.scm')
-rw-r--r--guix-data-service/model/utils.scm69
1 files changed, 62 insertions, 7 deletions
diff --git a/guix-data-service/model/utils.scm b/guix-data-service/model/utils.scm
index fc6d77c..d9b538b 100644
--- a/guix-data-service/model/utils.scm
+++ b/guix-data-service/model/utils.scm
@@ -120,7 +120,8 @@
data
#:key
sets-of-data?
- delete-duplicates?)
+ delete-duplicates?
+ use-temporary-table?)
(define field-strings
(map symbol->string fields))
@@ -177,7 +178,28 @@
field-strings)
" AND ")))
- (define (insert-sql missing-data)
+ (define (temp-table-select-query temp-table-name)
+ (string-append
+ "SELECT id, "
+ (string-join (map (lambda (field)
+ (string-append table-name "." field))
+ field-strings)
+ ", ")
+ " FROM " table-name
+ " INNER JOIN " temp-table-name
+ " ON "
+ (string-join
+ (map (lambda (field)
+ (string-append
+ "(" table-name "." field " = " temp-table-name "." field
+ " OR (" table-name "." field " IS NULL AND"
+ " vals." field " IS NULL))"))
+ field-strings)
+ " AND ")))
+
+ (define* (insert-sql missing-data
+ #:key
+ (table-name table-name))
(string-append
"INSERT INTO " table-name " ("
(string-join field-strings ", ")
@@ -227,11 +249,44 @@
data))
(let* ((existing-entries
- (exec-query->vhash conn
- select-query
- cdr
- (lambda (result)
- (string->number (first result)))))
+ (if use-temporary-table?
+ (let ((temp-table-name
+ (string-append "temp_" table-name))
+ (data
+ (if sets-of-data?
+ (delete-duplicates (concatenate data))
+ (if delete-duplicates?
+ (delete-duplicates data)
+ data))))
+ ;; Create a temporary table to store the data
+ (exec-query
+ conn
+ (string-append "CREATE TEMPORARY TABLE "
+ temp-table-name
+ " (LIKE "
+ table-name
+ " INCLUDING DEFAULTS)"))
+ ;; Populate the temporary table
+ (exec-query conn
+ (insert-sql data
+ #:table-name temp-table-name))
+ ;; Use the temporary table to find the existing values
+ (let ((result
+ (exec-query->vhash conn
+ temp-table-select-query
+ cdr
+ (lambda (result)
+ (string->number (first result))))))
+
+ (exec-query conn (string-append "DROP TABLE " temp-table-name))
+ result))
+
+ ;; If not using a temporary table, just do a single SELECT query
+ (exec-query->vhash conn
+ select-query
+ cdr
+ (lambda (result)
+ (string->number (first result))))))
(missing-entries
(filter (lambda (field-values)
(not (vhash-assoc