From: Christopher Baines <mail@cbaines.net>
To: 69292@debbugs.gnu.org
Cc: "Christopher Baines" <guix@cbaines.net>,
"Josselin Poiret" <dev@jpoiret.xyz>,
"Ludovic Courtès" <ludo@gnu.org>,
"Mathieu Othacehe" <othacehe@gnu.org>,
"Ricardo Wurmus" <rekado@elephly.net>,
"Simon Tournier" <zimon.toutoune@gmail.com>,
"Tobias Geerinckx-Rice" <me@tobias.gr>
Subject: [bug#69292] [PATCH 3/6] store: database: Inline SQL to where it's used.
Date: Tue, 20 Feb 2024 19:39:03 +0000 [thread overview]
Message-ID: <5abcabc4cb65ea63db7c4f046700116e4882b287.1708457946.git.mail@cbaines.net> (raw)
In-Reply-To: <4b6a268daab5e0b307dff2229d551a47c9fe1ebc.1708457946.git.mail@cbaines.net>
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
next prev parent reply other threads:[~2024-02-20 19:41 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-20 19:20 [bug#69292] [PATCH 0/6] Prepare the database code for use in the daemon Christopher Baines
2024-02-20 19:39 ` [bug#69292] [PATCH 1/6] store: database: Remove call-with-savepoint and associated code Christopher Baines
2024-02-20 19:39 ` [bug#69292] [PATCH 2/6] store: database: Remove with-statement " Christopher Baines
2024-02-23 16:35 ` Ludovic Courtès
2024-02-23 18:32 ` Reepca Russelstein via Guix-patches via
2024-03-05 11:05 ` Ludovic Courtès
2024-02-20 19:39 ` Christopher Baines [this message]
2024-02-23 16:40 ` [bug#69292] [PATCH 3/6] store: database: Inline SQL to where it's used Ludovic Courtès
2024-02-20 19:39 ` [bug#69292] [PATCH 4/6] store: database: Stop finalizing prepared statements Christopher Baines
2024-02-22 12:39 ` reepca--- via Guix-patches via
2024-02-26 10:50 ` Christopher Baines
2024-02-20 19:39 ` [bug#69292] [PATCH 5/6] store: database: Refactor sqlite-register Christopher Baines
2024-02-23 16:33 ` Ludovic Courtès
2024-02-20 19:39 ` [bug#69292] [PATCH 6/6] store: database: Rename a couple of procedures Christopher Baines
2024-02-23 16:43 ` Ludovic Courtès
2024-02-26 11:03 ` Christopher Baines
2024-02-27 9:24 ` Ludovic Courtès
2024-04-03 17:35 ` Christopher Baines
2024-02-23 16:31 ` [bug#69292] [PATCH 1/6] store: database: Remove call-with-savepoint and associated code Ludovic Courtès
2024-04-03 17:36 ` bug#69292: [PATCH 0/6] Prepare the database code for use in the daemon Christopher Baines
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5abcabc4cb65ea63db7c4f046700116e4882b287.1708457946.git.mail@cbaines.net \
--to=mail@cbaines.net \
--cc=69292@debbugs.gnu.org \
--cc=dev@jpoiret.xyz \
--cc=guix@cbaines.net \
--cc=ludo@gnu.org \
--cc=me@tobias.gr \
--cc=othacehe@gnu.org \
--cc=rekado@elephly.net \
--cc=zimon.toutoune@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).