From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id aC0LHlgA1WVc/AAAe85BDQ:P1 (envelope-from ) for ; Tue, 20 Feb 2024 20:41:12 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id aC0LHlgA1WVc/AAAe85BDQ (envelope-from ) for ; Tue, 20 Feb 2024 20:41:12 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1708458072; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=Myy85GcsqHOuLJqhH40yH9uvjJ40mb1qzV3RSc4wYv8=; b=Ngexb0jVcKpyCLInwGGPVybMzm/Vtie2zcrRyYpgkt6R1Qz7vqxtabn12OKheN7S+Hvn2n skqV8a8ZhInQVq6jJYWPfbFGm3qznjIEd0XqD4hoPbd8ivnCz9g/6Ul+ySI6/GUS0Z7cRC 7j584sufXEo1nydiO4uIje4RndWURjXsIpqAXQ9gud/kVoj7JnwNufoWEfNSjFWhTbx8We /97AjNP1zTN8or/ivxVDcmG7WsDQWDeZrvjGbLdDCEH5cA8i4NgH9T6gvmaQamHMmHUBlT ichkpFVqgbDVJLEj+j6P9cbanvuPhc2oWwKU5zQw1NCWn9RVzuQ8ju5D4QKcNQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Seal: i=1; s=key1; d=yhetil.org; t=1708458072; a=rsa-sha256; cv=none; b=SWq2DICbR3K4JbzcZURELsa8LjnDwwouwx/PBTpD/mCkDxOzNC8oF4ci1iHKf+xcKwmoqy +oGan/qP6zHkQEzOBmNNG20+mCa7nAjp9cjLrL7+alF+WfXUBvuxQatvNu5yGs5xWfFrQ9 CKLeUvErvZnSOvNBnmzui0TmPYRMTykvruE+ba7/U7DDPs6vl9ia2VJdlBOeDbsU9T1aXP P6HYZItdx60+iaZrv8iCKsTWbne/9m61xgqJ6tOyH6Ple0EYFBpWTfJvMyt7IZ3eK7Pxty uCt+XwINfcXfBRYsRwebw9zJ2lFku6Z7U+DV+l0norapDewhT5FzM3bGLE9acQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 4CC6B183EF for ; Tue, 20 Feb 2024 20:41:12 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rcVyy-0002sf-1a; Tue, 20 Feb 2024 14:40:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rcVys-0002kZ-AV for guix-patches@gnu.org; Tue, 20 Feb 2024 14:40:43 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rcVyq-0000ob-Nn; Tue, 20 Feb 2024 14:40:41 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rcVzC-0002V0-3F; Tue, 20 Feb 2024 14:41:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#69292] [PATCH 3/6] store: database: Inline SQL to where it's used. Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Tue, 20 Feb 2024 19:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69292 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 69292@debbugs.gnu.org Cc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?UTF-8?Q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 69292-submit@debbugs.gnu.org id=B69292.17084580109501 (code B ref 69292); Tue, 20 Feb 2024 19:41:02 +0000 Received: (at 69292) by debbugs.gnu.org; 20 Feb 2024 19:40:10 +0000 Received: from localhost ([127.0.0.1]:46638 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVyM-0002TA-5C for submit@debbugs.gnu.org; Tue, 20 Feb 2024 14:40:10 -0500 Received: from mira.cbaines.net ([212.71.252.8]:43150) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rcVyC-0002RT-6e for 69292@debbugs.gnu.org; Tue, 20 Feb 2024 14:40:01 -0500 Received: from localhost (unknown [212.132.255.10]) by mira.cbaines.net (Postfix) with ESMTPSA id B53DD27BBEA for <69292@debbugs.gnu.org>; Tue, 20 Feb 2024 19:39:07 +0000 (GMT) Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id c38561ed for <69292@debbugs.gnu.org>; Tue, 20 Feb 2024 19:39:07 +0000 (UTC) From: Christopher Baines Date: Tue, 20 Feb 2024 19:39:03 +0000 Message-ID: <5abcabc4cb65ea63db7c4f046700116e4882b287.1708457946.git.mail@cbaines.net> X-Mailer: git-send-email 2.41.0 In-Reply-To: <4b6a268daab5e0b307dff2229d551a47c9fe1ebc.1708457946.git.mail@cbaines.net> References: <4b6a268daab5e0b307dff2229d551a47c9fe1ebc.1708457946.git.mail@cbaines.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -3.48 X-Spam-Score: -3.48 X-Migadu-Queue-Id: 4CC6B183EF X-TUID: r5cEYw6ka4HZ This makes the code easier to read, as you don't have to keep jumping between the two places. * guix/store/database.scm (path-id-sql, update-sql, insert-sql, add-reference-sql): Remove variables. (path-id, update-or-insert, add-references): Include SQL. Change-Id: I53b4ab973be8d0cd10a0f35ba25972f1c9680353 --- guix/store/database.scm | 45 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/guix/store/database.scm b/guix/store/database.scm index de72b79860..7e3a2873ce 100644 --- a/guix/store/database.scm +++ b/guix/store/database.scm @@ -178,13 +178,14 @@ (define (last-insert-row-id db) ((#(id)) id) (_ #f)))) -(define path-id-sql - "SELECT id FROM ValidPaths WHERE path = :path") - (define* (path-id db path) "If PATH exists in the 'ValidPaths' table, return its numerical identifier. Otherwise, return #f." - (let ((stmt (sqlite-prepare db path-id-sql #:cache? #t))) + (let ((stmt (sqlite-prepare + db + " +SELECT id FROM ValidPaths WHERE path = :path" + #:cache? #t))) (sqlite-bind-arguments stmt #:path path) (let ((result (sqlite-fold cons '() stmt))) (sqlite-finalize stmt) @@ -192,14 +193,6 @@ (define* (path-id db path) ((#(id) . _) id) (_ #f))))) -(define update-sql - "UPDATE ValidPaths SET hash = :hash, registrationTime = :time, deriver = -:deriver, narSize = :size WHERE id = :id") - -(define insert-sql - "INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) -VALUES (:path, :hash, :time, :deriver, :size)") - (define-inlinable (assert-integer proc in-range? key number) (unless (integer? number) (throw 'wrong-type-arg proc @@ -222,14 +215,28 @@ (define* (update-or-insert db #:key path deriver hash nar-size time) (let ((id (path-id db path))) (if id - (let ((stmt (sqlite-prepare db update-sql #:cache? #t))) + (let ((stmt (sqlite-prepare + db + " +UPDATE ValidPaths +SET hash = :hash, + registrationTime = :time, + deriver = :deriver, + narSize = :size +WHERE id = :id" + #:cache? #t))) (sqlite-bind-arguments stmt #:id id #:deriver deriver #:hash hash #:size nar-size #:time time) (sqlite-fold cons '() stmt) (sqlite-finalize stmt) (last-insert-row-id db)) - (let ((stmt (sqlite-prepare db insert-sql #:cache? #t))) + (let ((stmt (sqlite-prepare + db + " +INSERT INTO ValidPaths (path, hash, registrationTime, deriver, narSize) +VALUES (:path, :hash, :time, :deriver, :size)" + #:cache? #t))) (sqlite-bind-arguments stmt #:path path #:deriver deriver #:hash hash #:size nar-size #:time time) @@ -237,13 +244,15 @@ (define* (update-or-insert db #:key path deriver hash nar-size time) (sqlite-finalize stmt) (last-insert-row-id db))))) -(define add-reference-sql - "INSERT OR REPLACE INTO Refs (referrer, reference) VALUES (:referrer, :reference);") - (define (add-references db referrer references) "REFERRER is the id of the referring store item, REFERENCES is a list ids of items referred to." - (let ((stmt (sqlite-prepare db add-reference-sql #:cache? #t))) + (let ((stmt (sqlite-prepare + db + " +INSERT OR REPLACE INTO Refs (referrer, reference) +VALUES (:referrer, :reference)" + #:cache? #t))) (for-each (lambda (reference) (sqlite-reset stmt) (sqlite-bind-arguments stmt #:referrer referrer -- 2.41.0