diff --git a/guix-data-service/model/utils.scm b/guix-data-service/model/utils.scm index 707c697..c3b0cee 100644 --- a/guix-data-service/model/utils.scm +++ b/guix-data-service/model/utils.scm @@ -37,6 +37,7 @@ insert-missing-data update-or-insert bulk-select + bulk-insert insert-and-return-id prepare-insert-and-return-id)) @@ -278,7 +279,8 @@ JOIN (VALUES " table-name fields data - #:key (id-proc string->number)) + #:key (id-proc string->number) + (returning '(id))) (define field-strings (map symbol->string fields)) @@ -298,33 +300,44 @@ INSERT INTO " table-name " (" (string-join field-strings ", ") ") VALUES ")")) data) ", ") " -ON CONFLICT DO NOTHING -RETURNING id")) +ON CONFLICT DO NOTHING" + (if (and returning + (not (null? returning))) + (string-append + " +RETURNING " (string-join (map symbol->string returning) + ", ")) + ""))) (if (null? data) #() - (let* ((query-result (exec-query conn query)) - (expected-ids (length data)) - (returned-ids (length query-result))) - (if (= expected-ids returned-ids) - (let ((result - (make-vector returned-ids))) - (fold - (lambda (row index) - (match row - ((id) - (vector-set! result index - (id-proc id)))) - (1+ index)) - 0 - query-result) - result) - ;; Can't match up the ids to the data, so just query for them - (bulk-select conn - table-name - fields - data - #:id-proc id-proc))))) + (if (and returning + (not (null? returning))) + (let* ((query-result (exec-query conn query)) + (expected-ids (length data)) + (returned-ids (length query-result))) + (if (= expected-ids returned-ids) + (let ((result + (make-vector returned-ids))) + (fold + (lambda (row index) + (match row + ((id) + (vector-set! result index + (id-proc id)))) + (1+ index)) + 0 + query-result) + result) + ;; Can't match up the ids to the data, so just query for them + (bulk-select conn + table-name + fields + data + #:id-proc id-proc))) + (begin + (exec-query conn query) + *unspecified*)))) (define* (insert-missing-data conn